从Android内部存储复制图像文件到相册

5

我有一个相机应用程序,用户拍摄照片后,我将其保存到我创建的目录中的内部存储器中。所有这些都很好,图像被保存在那里,我可以加载它并在之后显示,但是我在将图像保存到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);

这个文件是我保存在内部存储器中的图像。 使用此方法,所有我看到的只是在画廊中出现的破损图像。


1
真的没有人有解决方案吗? - TP89
3个回答

4

将它复制到画廊还是在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,它没有复制方法。 - TP89
抱歉,第二个示例使用的是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();" - ade sueb
我应该使用什么作为destFile?我尝试了这个“File storagePath = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); out = new FileOutputStream(storagePath + "/" + System.currentTimeMillis() +".JPEG");”,但是图片没有添加到相册中。 - TP89

0

你可以使用这个方法来保存图片:

  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(); 
            }               
        }

但这要求我必须将图像保存在内存中。当我想将图像保存到画廊时,图像已经被保存到文件中了。似乎没有必要再次加载图像只为了将其保存到另一个位置。 - TP89
为什么要将图像复制到相册???您可以直接在相机意图的OnActivityResult中将其保存到SD卡。 - Jas
1
在我的应用程序中,图像被发送到服务器,我不希望在确保服务器已接收到图像之前将其保存在图库中。从用户拍摄图像的时刻到它被发送到服务器之前,我需要通过3个活动,这就是为什么我已经将其保存到文件中的原因。 - TP89

0

这是可能会有帮助的:

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”错误。 - TP89

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接