[英]Failed to save image from app assets folder to Gallery Folder in Android?
我已將此代碼保存在指定的文件夾名稱中寫入此代碼,但我想將此圖像保存在Gallery / Specified FolderName / Image中 。有人可以幫助我..... 代碼:
saveImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
saveImage(getBitmapFromAsset("save_fatwa.jpg"),
"save_fatwa.jpg");
}
});
private Bitmap getBitmapFromAsset(String strName) {
AssetManager assetManager = getAssets();
InputStream istr = null;
Bitmap bitmap = null;
try {
istr = assetManager.open(strName);
if (istr.equals(null)) {
Log.i("getBitmapFromAsset isStr", "" + istr);
bitmap = BitmapFactory.decodeStream(assetManager
.open("save_fatwa.jpg"));
} else {
bitmap = BitmapFactory.decodeStream(istr);
}
} catch (IOException e) {
e.printStackTrace();
}
// Bitmap bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}
private void saveImage(Bitmap finalBitmap, String imageName) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Jamia Binoria Images");
myDir.mkdirs();
// String fname = "save_fatwa.jpg";
File file = new File(myDir, imageName);
if (file.exists()) {
Log.i("file exists", "" + imageName);
file.delete();
} else {
Log.i("file does not exists", "" + imageName);
}
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG,90,out);
// sendBroadcast(new Intent(
// Intent.ACTION_MEDIA_MOUNTED,
// Uri.parse("file://" +
// Environment.getExternalStorageDirectory())));
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
在這里,我使用了另一個功能來從Assets Folder.Image獲取圖像。請讓我知道在此代碼中添加哪些內容以將其保存在Gallery / FolderName / Image中
向mainfest.xml文件添加權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
這是一個鏈接檢查
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.