将图像文件从Android内部存储复制到gallery
我有一个摄像头应用程序,在用户拍摄图像后,我将其保存到我创建的目录中的内部存储器中。 所有这些都很好,图像保存在那里,我可以加载它并在之后显示,但我也很难将图像保存到Android gallery 我想做的是,将图像保存到内部目录后,将其复制到gallery 我试过这个:将图像文件从Android内部存储复制到gallery,android,android-gallery,Android,Android Gallery,我有一个摄像头应用程序,在用户拍摄图像后,我将其保存到我创建的目录中的内部存储器中。 所有这些都很好,图像保存在那里,我可以加载它并在之后显示,但我也很难将图像保存到Android gallery 我想做的是,将图像保存到内部目录后,将其复制到gallery 我试过这个: ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.DATE_TAKEN, System.current
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);
其中文件是我保存在内部存储器中的图像。
使用此方法,我只会在库中获得一个损坏的图像。您可以使用此方法保存图像:
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();
}
}
复制到gallery还是在android gallery中显示 如果你想在android gallery中显示图像?您可以通过扫描介质来完成此操作,以下是如何扫描:
Uri=Uri.fromFile(文件);
Intent scanFileIntent=新意图(
Intent.ACTION\u MEDIA\u SCANNER\u SCAN\u文件,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();
这可能会有所帮助:
Uri selectedImageURI = data.getData();
imageFile = new File(getRealPathFromURI(selectedImageURI));
获取路径后,u可以通过以下方式存储图像:
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);
希望这有帮助。但这需要我将图像存储在内存中。当我想将图像保存到库中时,图像已经保存到文件中。似乎没有必要再次加载图像以将其保存到其他位置。为什么要将图像复制到gallery???根据您的相机意图,您可以在OnActivity中将图像直接保存到sd卡。在我的应用程序中,图像会被发送到服务器,在我知道服务器收到图像之前,我不希望将图像保存在库中。从用户拍摄图像到将其发送到服务器,我经历了3个活动,这就是为什么我已经将其保存到文件中的原因。您的扫描代码似乎不起作用。对于第二个示例,您使用什么文件类?因为我得到的只是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”);”但是图像并没有添加到Gallerui中,实际上没有人有解决方案?context.getContentResolver().insert(图像文件,值); 将Uri作为其第一个参数,如果使用Uri.fromFile(file),则会抛出未知URL文件错误