【问题标题】:Copy image file from Android internal storage to gallery将图像文件从 Android 内部存储复制到图库
【发布时间】:2015-10-12 07:09:03
【问题描述】:

我有一个相机应用程序,在用户拍摄图像后,我将其保存到我创建的目录中的内部存储中。 一切正常,图像保存在那里,我可以加载它并在之后显示,但我也无法将图像保存到 Android 画廊。

我想要的是将图像保存到内部目录后,将其复制到图库。

我试过这个:

    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
    values.put(MediaStore.Images.Media.SIZE, file.length());
    values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());

    context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

文件是我保存在内部存储中的图像。 使用这种方法,我得到的只是图库中的损坏图像。

【问题讨论】:

  • 真的没有人有办法吗?

标签: android android-gallery


【解决方案1】:

将其复制到画廊或在 android 画廊中显示?

如果你想在 android 库中显示图像?您可以通过扫描媒体来做到这一点,这是如何扫描:

Uri uri = Uri.fromFile(file); Intent scanFileIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri); sendBroadcast(scanFileIntent);

如果您只想复制文件,请执行以下操作:

  in = new FileInputStream(sourceFile);
  out = new FileOutputStream(destFile);
  byte[] buffer = new byte[1024];
  int read;
  while ((read = in.read(buffer)) != -1) {
    out.write(buffer, 0, read);
  }
  in.close();
  in = null;

  out.flush();
  out.close();

【讨论】:

  • 您的扫描码似乎不起作用。您在第二个示例中使用什么 Files 类?因为我得到的只是 mediastore.files,它没有复制方法。
  • 对不起,第二个示例是使用 Java 7,可能无法在 Android 开发中实现。复制文件的基础是从文件源读取内容并将内容写入文件目标。这是一个使用 FileOutputStream "in = new FileInputStream(sourceFile); out = new FileOutputStream(destFile); byte[] buffer = new byte[1024]; int read; while ((read = in.read(buffer)) 的例子!= -1) { out.write(buffer, 0, read); } in.close(); in = null; out.flush(); out.close();"
  • 我应该使用什么作为 destFile?我试过这个“文件 storagePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); out = new FileOutputStream(storagePath + "/" + System.currentTimeMillis() +".JPEG");"但是图片没有添加到galleru
【解决方案2】:

您可以使用此方法保存图像:

  public void saveToSD(Bitmap outputImage){


            File storagePath = new File(Environment.getExternalStorageDirectory() + "/MyPhotos/"); 
            storagePath.mkdirs(); 

            File myImage = new File(storagePath, Long.toString(System.currentTimeMillis()) + ".jpg");

            try { 
                FileOutputStream out = new FileOutputStream(myImage); 
                outputImage.compress(Bitmap.CompressFormat.JPEG, 80, out); 
                out.flush();    
                out.close();
            } catch (Exception e) { 
                e.printStackTrace(); 
            }               
        }

【讨论】:

  • 但这需要我将图像保存在内存中。在我想将图像保存到图库时,图像已经保存到文件中。似乎没有必要为了将其保存到另一个地方而再次加载图像。
  • 为什么要将图片复制到图库???您可以在相机意图的 OnActivityResult 中直接将其保存到 sd 卡中。
  • 在我的应用程序中,图像被发送到服务器,我不希望图像保存在画廊中,直到我知道服务器收到它。从用户拍摄图像到将图像发送到服务器的那一刻,我经历了 3 个活动,这就是为什么我已经将它保存到一个文件中。
【解决方案3】:

这可能会有所帮助:

Uri selectedImageURI = data.getData();
imageFile = new File(getRealPathFromURI(selectedImageURI));

获取路径后,您可以通过以下方式存储图像:

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
values.put(MediaStore.Images.Media.SIZE, file.length());
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());

context.getContentResolver().insert(imageFile, values);

希望这会有所帮助。

【讨论】:

  • context.getContentResolver().insert(imageFile, values);将 Uri 作为其第一个参数,如果我使用 Uri.fromFile(file) 它会引发 Unknown URL file 错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-14
  • 2013-10-28
  • 1970-01-01
  • 2023-03-05
  • 2020-05-30
  • 2019-05-04
相关资源
最近更新 更多