[英]Cropping image and then saving it in gallery
我正在使用以下代码使用 android crop intent
裁剪图像。
Intent cropIntent = new Intent("com.android.camera.action.CROP", null)
.setDataAndType(picUri,"image/*")
.putExtra("crop", "true")
.putExtra("aspectX", 1)
.putExtra("aspectY", 1)
.putExtra("outputX", 128)
.putExtra("outputY", 128)
.putExtra("scale", true)
.putExtra("return-data", false)
.putExtra("scaleUpIfNeeded", true)
.putExtra(MediaStore.EXTRA_OUTPUT, picUri)
.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(cropIntent, PIC_CROP);
然后我从这段代码中获取裁剪后的图片,我想将它保存在图库中的一个单独的文件夹中(由我的应用程序创建)。
这是保存图像的代码:
public static String SaveImage(Bitmap finalBitmap)
{
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Shopic Snaps");
if(!myDir.exists())
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image_"+ n+ GenerateRandomName() +".jpg";
File file = new File (myDir, fname);
if (file.exists ())
file.delete ();
try
{
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return root + "/App Snaps/"+fname;
}
现在这个函数应该将图片保存在图库中并将图片的路径返回给我。代码运行正常,没有错误。
但是在我检查图库时执行任务后,它是空的。 我不知道为什么它没有将图片保存在画廊中。
编辑:
我在另一部手机上测试了该应用程序,图片在其中保存完好并显示出来。 所以我想这是我手机的问题,但我不知道是什么导致了这个问题。
// Initialize intent
Intent intent = new Intent("com.android.camera.action.CROP");
// set data type to be sent
intent.setType("image/*");
// get croppers available in the phone
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
// handle the case if there's no cropper in the phone
if (size == 0) {
Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
return;
} else {
// now this is the case if cropper exists
// initialize the Uri for the captures or gallery image
Uri imageUri = Uri.fromFile(new File(mCurrentPhotoPath));
// Send the Uri path to the cropper intent
intent.setData(imageUri);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
// Here's my attempt to ask the intent to save output data as file
File f = null;
// Here I initialize empty file
try{
// This returns the file created
f = setUpCroppedFile();
mCurrentThumbPath = f.getAbsolutePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
intent.putExtra("output", Uri.fromFile(f));
}
catch(IOException e){
e.printStackTrace();
}
intent.putExtra("return-data", false);
// --------------------------------------------------------------------
// --------------------------------------------------------------------
// If there's only 1 Cropper in the phone (e.g. Gallery )
if (size == 1) {
// get the cropper intent found
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(i, CROPPED_PHOTO);
}
检查文件资源管理器,文件是否已存储在设备中.. 如果文件已存储但未显示在图库中..? 试试这个 注意:按照内部和外部 SD 卡的步骤操作
转到文件管理器 -> Android -> 数据 -> com.android.gallery3d。 删除内部和外部 SD 卡中的文件夹 (com.android.gallery3d)。 转到设置 -> 应用程序/应用程序管理器 -> 搜索图库 -> 打开图库并点击清除数据。 关闭手机并等待几分钟(比如 2-3 分钟),然后打开并等待几分钟。 现在就是这样。 所有图像和视频都将显示在图库下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.