如果您想了解什么是SD卡或SQLLiteDB中android的最佳实践存储图像?的相关知识,那么本文是一篇不可错过的文章,我们将为您提供关于android–在CleanArchitecture中管理领
如果您想了解什么是SD卡或SQL Lite DB中android的最佳实践存储图像?的相关知识,那么本文是一篇不可错过的文章,我们将为您提供关于android – 在Clean Architecture中管理领域实例的最佳实践是什么?、android – 如何从存储在SD卡上的图像中获取图像路径、android – 如何在SD卡中保存GIF图像?、android – 如何在手机SD卡或其他地方搜索文件的有价值的信息。
本文目录一览:- 什么是SD卡或SQL Lite DB中android的最佳实践存储图像?
- android – 在Clean Architecture中管理领域实例的最佳实践是什么?
- android – 如何从存储在SD卡上的图像中获取图像路径
- android – 如何在SD卡中保存GIF图像?
- android – 如何在手机SD卡或其他地方搜索文件
什么是SD卡或SQL Lite DB中android的最佳实践存储图像?
我想存储样本图像.在SD卡或DB中存储天气的最佳方式是什么?
解决方法:
如果图像不大(2-3 KB)并且它们的集合不应该扩展,那么您可以将它们保存到db.但是编写它们会更难,阅读它们.另一方面,用户将无法对它们进行任何操作.
否则,如果您想让用户扩展图像集或图像相当大,您应该将它们存储到SD卡.
无论如何,你应该意识到空间不足并适当地对待这些案件.
android – 在Clean Architecture中管理领域实例的最佳实践是什么?
我的项目使用干净的架构.在这种情况下,UI层与Domain层分开.所以我认为UI层不拥有realm实例会更好.正如领域的文档建议在Activity的生命周期中管理领域实例,那么我应该如何处理领域实例呢?
为了更清楚,我的项目太重了,无法更改所有对象扩展的RealmObject.所以我使用单独的对象来持久化数据.当api调用完成时,业务对象转换为领域对象,与来自领域的查询相反.我创建这样的方法:
public void insert(T object){
final Realm realm = RealmProvider.getRealm();
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.copyToRealmOrUpdate(createRealmObject(object));
}
},new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
realm.close();
}
},new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
realm.close();
}
});
}
实际上,它工作正常.但下面我不知道如何处理关闭领域实例.
public ObservableirstAsync()
.asObservable()
.filter(new Func1omrealm(realmObject);
}
});
}
Realm对象绑定到realm实例,这意味着如果从realm实例检索对象然后关闭该实例(必须这样做),则无法再使用该对象.这违背了使用Realm的全部目的.
如果您要使用Realm,您应该将领域逻辑与您的活动/服务等紧密相关,并且不要试图将其隐藏在单独的层中,以便您可以完全控制它.
.map(new Func1omrealm(realmObject);
realm.close();
return o;
}
});
android – 如何从存储在SD卡上的图像中获取图像路径
Cursor cur = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,null,null); startManagingCursor(cur); cur.movetoFirst(); while (cur.movetoNext()) { String str = cur.getString(cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_disPLAY_NAME)); Log.e("#########",str); }
这似乎就像返回目录名,
我也见过这段代码:
File images = Environment.getDataDirectory(); File[] imagelist = images.listFiles(new FilenameFilter(){ public boolean accept(File dir,String name) { return ((name.endsWith(".jpg"))||(name.endsWith(".png"))); } }); String []mFiles = new String[imagelist.length]; for(int i= 0 ; i< imagelist.length; i++) { mFiles[i] = imagelist[i].getAbsolutePath(); } Uri[] mUrls = new Uri[mFiles.length]; for(int i=0; i < mFiles.length; i++) { mUrls[i] = Uri.parse(mFiles[i]); Log.e("###############","MURIS: "+mUrls[i].getEncodedpath()); }
但这是抛出一个nullpointer异常.
解决方法
FileFilter filterForImageFolders = new FileFilter() { public boolean accept(File folder) { try { //Checking only directories,since we are checking for files within //a directory if(folder.isDirectory()) { File[] listofFiles = folder.listFiles(); if (listofFiles == null) return false; //For each file in the directory... for (File file : listofFiles) { //Check if the extension is one of the supported filetypes //imageExtensions is a String[] containing image filetypes (e.g. "png") for (String ext : imageExtensions) { if (file.getName().endsWith("." + ext)) return true; } } } return false; } catch (SecurityException e) { Log.v("debug","Access Denied"); return false; } } };
编辑:澄清一下,使用它,你会做如下的事情:
File extStore = Environment.getExternalStorageDirectory(); File[] imageDirs = extStore.listFiles(filterForImageFolders);
android – 如何在SD卡中保存GIF图像?
这是我显示gif图像的代码
//Save code save.setonClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.gpp3); File outputFile = new File("/sdcard/gpp3.gif"); FileOutputStream fos = null; try { fos = new FileOutputStream(outputFile); } catch (FileNotFoundException e) { e.printstacktrace(); } if (fos != null) { AnimatedGifEncoder gifEncoder = new AnimatedGifEncoder(); gifEncoder.start(fos); gifEncoder.addFrame(image); gifEncoder.finish(); } } });
那么,上面的代码有什么问题.请告诉我.
解决方法
private void saveGIF() { try { File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"shared_gif_shai" + System.currentTimeMillis() + ".gif"); long startTime = System.currentTimeMillis(); Log.d(TAG,"on do in background,url open connection"); InputStream is = getResources().openRawResource(R.drawable.g); Log.d(TAG,url get input stream"); BufferedInputStream bis = new BufferedInputStream(is); Log.d(TAG,create buffered input stream"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Log.d(TAG,create buffered array output stream"); byte[] img = new byte[1024]; int current = 0; Log.d(TAG,write byte to baos"); while ((current = bis.read()) != -1) { baos.write(current); } Log.d(TAG,done write"); Log.d(TAG,create fos"); FileOutputStream fos = new FileOutputStream(file); fos.write(baos.toByteArray()); Log.d(TAG,write to fos"); fos.flush(); fos.close(); is.close(); Log.d(TAG,done write to fos"); } catch (Exception e) { e.printstacktrace(); } }
并且还在您的AndroidMenifest.xml文件中授予此权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
android – 如何在手机SD卡或其他地方搜索文件
我试过搜索但找不到任何直接的API.
是否有特定的API,或者是否有实现相同的繁琐方法.
或者是否有一种机制来调用linux调用find或类似的东西
谢谢
解决方法
关于什么是SD卡或SQL Lite DB中android的最佳实践存储图像?的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于android – 在Clean Architecture中管理领域实例的最佳实践是什么?、android – 如何从存储在SD卡上的图像中获取图像路径、android – 如何在SD卡中保存GIF图像?、android – 如何在手机SD卡或其他地方搜索文件等相关知识的信息别忘了在本站进行查找喔。
本文标签: