2016-04-07 42 views
1

到目前爲止,我所取得的成就是,我可以存儲圖像從相機點擊到一個新的文件夾商店從畫廊到不同的文件夾中的圖像

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     f = new File(Utils.getPath(), new Date().getTime() + ".jpg"); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
     startActivityForResult(intent, TAKE_PHOTO); 

但我不知道如何存儲從圖庫中選擇一個圖片到我創建的同一個文件夾。請幫幫我。 預先感謝您。

+3

你正在創建你的項目名稱的文件夾,你只需將捕獲的圖像複製到你的項目名稱文件夾?想要你先澄清.. –

+0

本主題可能會幫助您:http://stackoverflow.com/questions/11846108/android-saving-bitmap-to-sd-card –

+0

Utils.getPath()返回我想要創建一個新文件夾來存儲的位置圖片。休息是由意圖處理的。但是當您從圖庫中選擇圖像時,MediaStore.EXTRA_OUTPUT不起作用。那麼如何將從圖庫中選擇的圖像存儲到另一個文件夾? – AmeyaG

回答

4

首先,從您從圖庫中獲取的URI獲取真實路徑。

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); 
} 

現在的圖像複製到其他位置,

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(); 
       } 
    } 



    File destFile = new File("Dest path"); 

    copyFile(new File(getPath(data.getData())), destFile); 

退房網址瞭解更多詳情,

  1. How to Copy Image File from Gallery to another folder programmatically in Android

  2. Android copy image from gallery folder onto SD Card alternative folder

+0

這工作完美 – AmeyaG

0

上述解決方案對我的作品,但有細微的變化:

相機單擊圖像由ACTION_IMAGE_CAPTURE意圖保存。而從我們自己的應用程序中從圖庫中選取圖像,我們需要保存來自ACTION_PICKT意圖的圖像。在這種情況下保存基本上是將圖像從圖庫複製到您的ImageView中後顯示在您自己的應用程序文件夾中。

首先你需要有目標文件:所以調用下面的方法來讓你的應用程序的目錄是在安卓>資料>您的應用程序文件夾>文件>圖片:

private File mPhotoFile; 
mPhotoFile=PhotoLab.get(getActivity()).getPhotoFile(mPhoto); 

這裏是getPhotoFile()函數

public File getPhotoFile(Photo photo){ 
    File externalFilesDir=mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);//getExternalStorageDirectory().toString()); 

    if(externalFilesDir==null){ 
     return null; 
    } 
    return new File(externalFilesDir, photo.getPhotoFilename()); 
} 

現在,您已經有了您想要保存Image Intent所選圖像的文件。現在調用ACTION_PICKT意圖。

private static final int PICK_IMAGE_REQUEST=2; 
Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //Trying intent to pick image from gallery 
        pickIntent.setType("image/*"); 
        Uri uriImagePath = Uri.fromFile(mPhotoFile); 
        pickIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriImagePath); 
        startActivityForResult(pickIntent, PICK_IMAGE_REQUEST); 

接收結果在onActivityResult繼後是我的代碼:

if(requestCode==PICK_IMAGE_REQUEST){ 
     Picasso.with(getActivity()).load(data.getData()).fit().into(mPhotoView); 
     File file=getBitmapFile(data); 


     try { 
      copyFile(file, mPhotoFile); 
     }catch(IOException e){ 
      Toast.makeText(getActivity(),"Cannot use Gallery, Try Camera!",Toast.LENGTH_SHORT).show(); 
      e.printStackTrace(); 
     } 
    } 

獲取數據後,我通過它進入下一個方法來查找Intent.ACTION_PICK拾取圖像的絕對路徑。以下是方法定義:

public File getBitmapFile(Intent data){ 
    Uri selectedImage=data.getData(); 
    Cursor cursor=getContext().getContentResolver().query(selectedImage, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null); 
    cursor.moveToFirst(); 

    int idx=cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    String selectedImagePath=cursor.getString(idx); 
    cursor.close(); 

    return new File(selectedImagePath); 

} 

得到所選圖像的絕對路徑後。我調用了copyFile()函數。具體如下。請記住,我已經生成了我的新文件路徑。

public File getBitmapFile(Intent data){ 
    Uri selectedImage=data.getData(); 
    Cursor cursor=getContext().getContentResolver().query(selectedImage, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null); 
    cursor.moveToFirst(); 

    int idx=cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    String selectedImagePath=cursor.getString(idx); 
    cursor.close(); 

    return new File(selectedImagePath); 

} 

我的目的地是mPhotoFile。我們首先生成的。 如果您想從圖庫中挑選圖像並將其存儲,以便在您每次打開應用程序時在應用程序的imageView中進一步顯示,則此整個代碼將起作用。