在保存图像到画廊从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();
}
});
2条答案
按热度按时间pgx2nnw81#
4c8rllxm2#
按照此代码从图像视图下载图像
{ if(上下文兼容.检查自我权限(此,清单.权限.写入_外部_存储)==包管理器.权限_已授予){ try {瓦尔bitmap = imageView.getDrawable().toBitmap()