【发布时间】:2013-07-12 17:40:20
【问题描述】:
我正在使用以下代码使用 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;
}
现在这个函数应该将图片保存在图库中并返回图片的路径。代码运行正常,没有错误。
但是当我检查画廊时执行任务后,它是空的。我不知道为什么它没有将图片保存在图库中。
编辑:
我在另一部手机上测试了该应用程序,图片在其中保存良好并显示出来。所以我猜这是我手机的问题,但我不知道是什么导致了问题。
【问题讨论】:
-
文件是否存储在所需的目录中,是否有效?如果是这样,请阅读stackoverflow.com/questions/13270789/…
-
文件没有存储在那里,但有时会,我不知道什么时候有,什么时候没有
-
您知道在您的代码中返回的目录与您存储文件的目录不同(“Shopic Snaps”“App Snaps”)?
-
Android 没有
CROPIntent: commonsware.com/blog/2013/01/23/… -
抱歉回复晚了,哦其实我不小心改了路径。抱歉,顺便说一句,它在代码中没问题,而且图片没有在图库中显示。问题是它没有将图片保存在手机图库中,尽管代码可以正常工作
标签: android image image-processing android-gallery