2011-04-15 38 views
1

我遇到问题。 我从android中的图库中选择图像并在imageview中显示它。 我想要做的是用另一个名称创建另一个文件,并将此映像复制到我的应用程序资源中的目录中的此文件。 任何人都可以帮助我。从可绘制的图形创建新的图像文件

回答

2

您可以用下面的启动画廊选择器意图:

public void imageFromGallery() { 
Intent getImageFromGalleryIntent = 
    new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
startActivityForResult(getImageFromGalleryIntent, SELECT_IMAGE); 
} 

然后当它返回时,获得所选择的图像与下面的代码部分的路径:

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) { 
    switch(requestCode) { 
    case SELECT_IMAGE: 
     mSelectedImagePath = getPath(data.getData()); 
     break; 
    } 
} 

public String getPath(Uri uri) { 
String[] projection = { MediaStore.Images.Media.DATA }; 
Cursor cursor = managedQuery(uri, projection, null, null, null); 
startManagingCursor(cursor); 
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
cursor.moveToFirst(); 
return cursor.getString(column_index); 
} 

现在,您可以将字符串中的路径名复制到其他位置。

try { 
File sd = Environment.getExternalStorageDirectory(); 
File data = Environment.getDataDirectory(); 
if (sd.canWrite()) { 
    String sourceImagePath= "/path/to/source/file.jpg"; 
    String destinationImagePath= "/path/to/destination/file.jpg"; 
    File source= new File(data, souceImagePath); 
    File destination= new File(sd, destinationImagePath); 
    if (source.exists()) { 
     FileChannel src = new FileInputStream(source).getChannel(); 
     FileChannel dst = new FileOutputStream(destination).getChannel(); 
     dst.transferFrom(src, 0, src.size()); 
     src.close(); 
     dst.close(); 
    } 
} catch (Exception e) {} 

这将做你的工作,我认为..

而且通过这个link