2011-02-07 68 views

回答

25

Usmaan,

您可以启动画廊选择器意图如下:

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, sourceImagePath); 
     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) {} 
+0

对不起,我不明白,我知道要热在我的应用程序中拍摄照片等...但后来我想从图库文件夹复制图像到不同的文件夹,即时通讯不知道这段代码是否这样做呢? – Beginner 2011-02-07 14:10:47

1

图库图像已经存储在Android手机的SD卡中。官方文档在working with external storage上有一个很好的部分,你应该检查一下。

+0

抱歉,我的意思是说像从该文件夹复制到antoher – Beginner 2011-02-07 12:28:36

相关问题