Skip to content

Instantly share code, notes, and snippets.

@beeender
Created December 28, 2015 07:06
Show Gist options
  • Save beeender/3172b31a30b1a67cb095 to your computer and use it in GitHub Desktop.
Save beeender/3172b31a30b1a67cb095 to your computer and use it in GitHub Desktop.
Create Realm on external storage for Android M
package apptest.realm.io.androidmtest;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.io.File;
import io.realm.Realm;
import io.realm.RealmConfiguration;
public class MainActivity extends AppCompatActivity {
RealmConfiguration myConfig;
Realm realm;
final private int REQUEST_WRITE_STORAGE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
boolean hasPermission = (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_STORAGE);
} else {
openRealm();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_WRITE_STORAGE:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
openRealm();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast toast = Toast.makeText(this,
"Write permission to the external storage is needed for this app", Toast.LENGTH_LONG);
toast.show();
}
return;
default:
break;
}
}
private void openRealm() {
File file = getExternalFilesDir("/MyApp/Db/");
if (!file.exists()) {
boolean result = file.mkdir();
Log.e("TTT", "Results: " + result);
}
myConfig = new RealmConfiguration.Builder(file)
.name("test" + ".realm")
.build();
Realm.setDefaultConfiguration(myConfig);
realm = Realm.getDefaultInstance();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment