io.realm.exceptions.RealmIOException:无法打开3
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了io.realm.exceptions.RealmIOException:无法打开3相关的知识,希望对你有一定的参考价值。
我试图为我的领域数据库做备份,然后我得到了这个错误并指出realms.writeCopyTo(exportRealmFile);
。我想也许我得到了这个错误,因为我试图将3个领域放在一个文件或类似的东西中。如果是这样,我如何在一个文件中进行3个域备份?如果不是我怎么能让它运行?
这是错误
E/androidRuntime: FATAL EXCEPTION: main
Process: com.jmnapps.expensetracker, PID: 4987
io.realm.exceptions.RealmIOException: Failed to open 3
at io.realm.internal.Group.nativeWriteToFile(Native Method)
at io.realm.internal.Group.writeToFile(Group.java:216)
at io.realm.Realm.writeEncryptedCopyTo(Realm.java:1077)
at io.realm.Realm.writeCopyTo(Realm.java:1057)
at com.jmnapps.expensetracker.ui.accounts.AccountsFragment.BackupCategories(AccountsFragment.java:281)
at com.jmnapps.expensetracker.ui.accounts.AccountsFragment$3.onClick(AccountsFragment.java:208)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
这是我执行领域备份的代码
public static AccountsFragment newInstance(){
return new AccountsFragment();
// Required empty public constructor
}
public AccountsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_account, container, false);
btnSync = (Button)rootView.findViewById(R.id.btnsync);
btnRestore = (Button)rootView.findViewById(R.id.btnrestore);
btnBackup = (Button)rootView.findViewById(R.id.btnBackup);
btnLogout=(Button)rootView.findViewById(R.id.btnLogout);
userPicture=(CircleImageView)rootView.findViewById(R.id.userprofilepic);
userName = (TextView)rootView.findViewById(R.id.personName);
userEmail=(TextView)rootView.findViewById(R.id.personEmail);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mMainActivityListener.setMode(MainActivity.NAVIGATION_MODE_STANDARD);
mMainActivityListener.setTitle("Account");
//GoogleApiClient client = new GoogleApiClient.Builder(getContext()).addApi(AppIndex.API).build();
mCategoryList = new ArrayList<>();
// mCategoriesAdapter = new CategoriesAdapter(mCategoryList, this);
RealmConfiguration realmConfig = new RealmConfiguration.Builder(getContext()).build();
// Get a Realm instance for this thread
realm = Realm.getInstance(realmConfig);
expenseList = realm.where(Expense.class).findAll();
categoryList = realm.where(Category.class).findAll();
reminderList = realm.where(Reminder.class).findAll();
String facebookUserId = "";
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String fbuserName = user.getDisplayName();
String fbuserEmail = user.getEmail();
Uri fbuserPhoto = user.getPhotoUrl();
// find the Facebook profile and get the user's id
for(UserInfo profile : user.getProviderData()) {
// check if the provider id matches "facebook.com"
if(FacebookAuthProvider.PROVIDER_ID.equals(profile.getProviderId())) {
facebookUserId = profile.getUid();
}
}
String photoUrl = "https://graph.facebook.com/" + facebookUserId + "/picture?height=500";
userName.setText(fbuserName);
userEmail.setText(fbuserEmail);
Picasso.get()
.load(fbuserPhoto)
.into(userPicture);
//userName.setText(fbuserName);
//userEmail.setText(fbuserEmail);
//Picasso.get()
//.load(fbuserPhoto)
//.into(userPicture);
btnSync.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
btnRestore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
btnBackup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BackupCategories();
}
});
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dlogout = new Dialog(getContext());
Dlogout.setContentView(R.layout.layout_logout);
Dlogout.setTitle("Personal Expense Tracker");
btnOkay = (Button)Dlogout.findViewById(R.id.btnokay);
btnOkay.setEnabled(true);
btnCancel=(Button)Dlogout.findViewById(R.id.btncancel);
btnCancel.setEnabled(true);
btnOkay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userName.setText("");
userEmail.setText("");
userPicture.setImageResource(R.drawable.defaultuser);
eraseCategories();
//SIGN OUT
FirebaseAuth.getInstance().signOut();
LoginManager.getInstance().logOut();
Intent i = new Intent(getActivity(), SignInActivity.class);
startActivity(i);
((Activity) getActivity()).overridePendingTransition(0, 0);
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dlogout.dismiss();
}
});
Dlogout.show();
}
});
}
public void eraseCategories() {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmResults<Expense> expdel = realm.where(Expense.class).findAll();
RealmResults<Category> catdel = realm.where(Category.class).findAll();
RealmResults<Reminder> remdel = realm.where(Reminder.class).findAll();
expdel.clear();
catdel.clear();
remdel.clear();
}
});
}
public void BackupCategories() {
RealmConfiguration realmConfig = new RealmConfiguration.Builder(getApplicationContext()).build();
Realm realms = Realm.getInstance(realmConfig);
File exportRealmFile;
EXPORT_REALM_PATH.mkdirs();
try {
// create a backup file
exportRealmFile = new File(EXPORT_REALM_PATH, EXPORT_REALM_FILE_NAME);
// if backup file already exists, delete it
exportRealmFile.delete();
// copy current realm to backup file
realms.writeCopyTo(exportRealmFile);
} catch (IOException e) {
e.printStackTrace();
}
String msg = "File exported to Path: " + EXPORT_REALM_PATH + "/" + EXPORT_REALM_FILE_NAME;
Toast.makeText(activity.getApplicationContext(), msg, Toast.LENGTH_LONG).show();
Log.d(TAG, msg);
realms.close();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
}
答案
写入条件以在删除之前检查备份文件是否存在。您每次尝试存储时都会删除,因此文件不会退出。
另一答案
试试这个。这对我来说可以。
public static void SaveFile(String folder, String file, byte[] data) {
try {
File del = new File(folder+file);
//check if the file exist
if (del.exists())
del.delete();
//create new folder if not exist
createRep(folder);
//save the file
FileOutputStream out = new FileOutputStream(folder+file);
out.write(data);
out.close();
}
catch (Exception e) {
Log.e("err", "error");
}
}
private static void createRep(String directory){
boolean isRepExists = false;
File sdcard = new File("/sdcard");
String[] sdcardContent = sdcard.list();
int i=0;
while(i<sdcardContent.length && !isRepExists){
if(sdcardContent[i].compareTo(directory)==0)
isRepExists = true;
i++;
}
File rep = new File(directory);
if(isRepExists)
rep.delete();
rep.mkdirs();
}
使用您的参数调用SaveFile(....)
方法。
另一答案
代码工作正常我只需要使用物理Android设备而不是模拟器。因为它在存储目录中有问题
以上是关于io.realm.exceptions.RealmIOException:无法打开3的主要内容,如果未能解决你的问题,请参考以下文章