[英]Unable to save imageview image to gallery..(eglSurfaceAttrib not implemented)..(android)
[英]unable to save image to gallery from ImageView in Android
將圖像從 webview 保存到圖庫時遇到問題......已授予存儲權限,但它不起作用......我已經嘗試了所有我能做的......請幫助我
這是下面的代碼
private void saveImageToExternalStorage(Bitmap finalBitmap) {
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/ahmed");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try
{
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
// Tell the media scanner about the new file so that it is
MediaScannerConnection.scanFile(getActivity(), new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}
這是按鈕 onClick()
cvDownload.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
anImage = ((BitmapDrawable) ivPhoto.getDrawable()).getBitmap();
saveImageToExternalStorage(anImage);
Toast.makeText(getContext(), "Download successfuly"+anImage.toString(),
Toast.LENGTH_SHORT).show();
}
});
it is work for me:
public void onCreate(){
File path=saveBitmap(bitmap);
MediaScannerConnection.scanFile(this, new String[]{path.getPath()}, new String[]{"png", "jpg"}, new MediaScannerConnection.OnScanCompletedListener() {
/*
* (non-Javadoc)
* @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
*/
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
// sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(path)));
startActivity(intent); /** replace with your own uri */
// finish();
// finishAffinity();
}
});
}
public static File saveBitmap(Bitmap bitmap) {
String fileName = "";
FileOutputStream outStream = null;
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/MyImages");
dir.mkdirs();
fileName = String.format("%d.jpeg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
try {
outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return outFile;
}
按照此代碼從圖像視圖下載圖像
{ if(ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { try { val bitmap = imageView.getDrawable().toBitmap()
var outStream: FileOutputStream? = null
val sdCard: File = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
val dir = File(sdCard.getAbsolutePath().toString() + "/sastana")
dir.mkdirs()
val fileName = String.format("%d.jpg", System.currentTimeMillis())
val outFile = File(dir, fileName)
outStream = FileOutputStream(outFile)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream)
outStream.flush()
outStream.close()
Toast.makeText(this, "Saved Successfully", Toast.LENGTH_SHORT).show()
}catch (e : Exception){
Log.e("qrcode",""+e)
}
} else {
Log.e("qrcode","permission not granted")
}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.