2011-02-07 75 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

相關問題