我想从画廊中挑选图像,并将其复制到SDCard的其他文件夹中。
从图库中选取图像的代码
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);
我将此URI onActivityResult获取到content://media/external/images/media/681
。
我想复制图像,
表单path ="content://media/external/images/media/681
在Android中path = "file:///mnt/sdcard/sharedresources/
sdcard的这条路径。
该怎么做呢?
发布于 2011-12-29 14:16:30
OutputStream out;
String root = Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
File createDir = new File(root+"Folder Name"+File.separator);
if(!createDir.exists()) {
createDir.mkdir();
}
File file = new File(root + "Folder Name" + File.separator +"Name of File");
file.createNewFile();
out = new FileOutputStream(file);
out.write(data);
out.close();
希望能对你有所帮助
发布于 2011-12-30 14:39:05
感谢所有人..。工作代码在这里..
private OnClickListener photoAlbumListener = new OnClickListener(){
@Override
public void onClick(View arg0) {
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
imagepath = Environment.getExternalStorageDirectory()+"/sharedresources/"+HelperFunctions.getDateTimeForFileName()+".png";
uriImagePath = Uri.fromFile(new File(imagepath));
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT,uriImagePath);
photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.PNG.name());
photoPickerIntent.putExtra("return-data", true);
startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch(requestCode){
case 22:
Log.d("onActivityResult","uriImagePath Gallary :"+data.getData().toString());
Intent intentGallary = new Intent(mContext, ShareInfoActivity.class);
intentGallary.putExtra(IMAGE_DATA, uriImagePath);
intentGallary.putExtra(TYPE, "photo");
File f = new File(imagepath);
if (!f.exists())
{
try {
f.createNewFile();
copyFile(new File(getRealPathFromURI(data.getData())), f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
startActivity(intentGallary);
finish();
break;
}
}
}
private void copyFile(File sourceFile, File destFile) throws IOException {
if (!sourceFile.exists()) {
return;
}
FileChannel source = null;
FileChannel destination = null;
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
if (destination != null && source != null) {
destination.transferFrom(source, 0, source.size());
}
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Video.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
发布于 2015-07-13 16:41:04
在阅读this link时,他们在这里讨论了四种在Java中复制文件的方法,因此也适用于android。
虽然作者的结论是使用@Prashant答案中使用的'channel‘是最好的方法,但您甚至可以探索其他方法。
(我已经尝试了前两个,它们都能找到)
https://stackoverflow.com/questions/8664440
复制相似问题