2016-07-14 29 views
0

我寫這段代碼和我的代碼從庫中選擇一張圖片並從中獲取數據但我不知道如何從Inputstrem或數據獲取圖像地址並將其存儲?如何從android庫中獲取地址

public void loadPic() 
{ 
    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
    photoPickerIntent.setType("image/*"); 
    startActivityForResult(photoPickerIntent, 1); 
} 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if(requestCode==1) 
    { 
     try { 
      Uri selectedImage=data.getData(); 
      InputStream inputStream = getContentResolver().openInputStream(selectedImage); 
      listItems.add(inputStream.toString()); 
     } catch (FileNotFoundException e) { 

      e.printStackTrace(); 
     } 
    } 
} 
+0

爲什麼你需要從圖庫中選擇圖像的路徑?你可以用'onActivityResult'回調中的Intent得到的'InputStream'完成你想做的任何事情。 –

+0

使用BitmapFactory獲取位圖,一致 bmp = BitmapFactory.decodeStream(inputStream); – prGD

+0

真的!我可以將它複製到另一個地方嗎 – Amir133

回答

1

讀取文件名稱,如下所示,相應地使用它。

  Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      cursor.moveToFirst(); 
      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String picturePath = cursor.getString(columnIndex); 
      File f = new File(picturePath); 
      String imageName = f.getName(); 
0

我不知道這是否是你想要什麼,但你可以得到這樣的形象:

Uri selectedImage = data.getData(); 
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage); 

一旦你的位圖,你可以看到如何存儲here它。

編輯:試試這個

 Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null); 
     cursor.moveToFirst(); 
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     cursor.close(); 
     ImageView imageView = (ImageView) findViewById(R.id.imgView); 
     imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

here服用。

+0

我需要保存地址以備下次使用。 – Amir133

+0

檢查編輯@ Amir133。讓我知道如果這不起作用,或者你需要更多的幫助。 –

0

有時,您無法從您選擇的圖片中獲取文件。 這是因爲選擇的來自Google+,Drive,Dropbox或任何其他提供商。

最好的解決辦法是要求系統通過Intent.ACTION_GET_CONTENT選擇一個內容,並將結果與​​內容提供者進行比較。

public void pickImage() { 
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("image/*"); 
    startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) { 
     if (data == null) { 
      //Display an error 
      return; 
     } 
     InputStream inputStream = context.getContentResolver().openInputStream(data.getData()); 
     //Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap... 
    } 
} 
+0

你的答案在「編譯器」有問題說我們沒有這個類,並說改爲「上下文」,但什麼時候顯示此錯誤「無法從類型上下文的靜態引用非靜態方法getContentResolver() – Amir133