【问题标题】:Cropping image and then saving it in gallery裁剪图像,然后将其保存在图库中
【发布时间】: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 没有CROP Intent: commonsware.com/blog/2013/01/23/…
  • 抱歉回复晚了,哦其实我不小心改了路径。抱歉,顺便说一句,它在代码中没问题,而且图片没有在图库中显示。问题是它没有将图片保存在手机图库中,尽管代码可以正常工作

标签: android image image-processing android-gallery


【解决方案1】:
// 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);
    }

【讨论】:

  • 所以你没有指向我的答案,而是复制并粘贴了答案!!
  • 什么是 setUpCroppedFile()?
【解决方案2】:

检查文件资源管理器,文件是否存储在设备中..如果文件存储但未显示在图库中..?尝试这个 注意:按照内部和外部 SD 卡的步骤操作

转到文件管理器 -> Android -> 数据 -> com.android.gallery3d。 删除内部和外部 SD 卡中的文件夹 (com.android.gallery3d)。 转到设置 -> 应用程序/应用程序管理器 -> 搜索图库 -> 打开图库并点击清除数据。 关闭手机并等待几分钟(例如 2-3 分钟),然后打开并等待几分钟。 现在就是这样。所有图片和视频都将显示在图库下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-03
    • 2021-07-20
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    相关资源
    最近更新 更多