2013-08-27 33 views
0

我嘗試了幾乎所有可能的方法,將圖像從存儲的文件路徑設置爲image-view從文件路徑設置位圖到imageview不起作用

沒有一個工作。 圖像是從其他活動的圖庫中選擇的,圖像路徑存儲在數據庫中。

圖像是類型的.JPG存儲在數據庫的路徑而沒有任何擴展的.jpeg .PNG

圖像路徑

eg. /external/storage/media/carimagename 

當我嘗試使用此代碼圖像設置爲imageview沒有錯誤,但圖像未設置。

for(int i=0;i<n;i++) 
{ 
Bitmap pic = BitmapFactory.decodeFile(listview_arraypro1[i]); 
carpict.setImageBitmap(pic); 

    /// File imgFile =new File("listview_arraypro1[i]"); 
    // carpict.setImageBitmap(BitmapFactory.decodeFile(imgFile.getAbsolutePath())); 



      // Bitmap carpic = BitmapFactory.decodeStream(is); 
      // carpic = Bitmap.createScaledBitmap(carpic, 72, 70, false); 

} 

我怎麼可以設定路徑格式/external/storage/media/carimagename圖像到imageview

即時通訊使用下列以獲得在以前的活動圖像路徑

   Uri selectedImage = imageReturnedIntent.getData(); 

      String selectedImagePath = selectedImage.getPath(); 

我收到來自selectedImagePath走錯路?

這是我得到了使用此路徑它沒有MNT/SD卡

private String getRealPathFromURI(Uri contentURI) { 
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null); 
    if (cursor == null) { // Source is Dropbox or other similar local file path 
     return contentURI.getPath(); 
    } else { 
     cursor.moveToFirst(); 
     int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
     return cursor.getString(idx); 
    } 
} 



    File imageFile = new File(getRealPathFromURI(selectedImage)); 
      String path= imageFile.toString(); 

回答

4

主要的事情是,首先,你必須在你的圖像路徑需要提到你的圖像類型...

再之後,你可以檢索使用的ImagePath:

String img_path = "image will be with image type";  
Bitmap bmp= BitmapFactory.decodeFile(img_path); 
ImageView iv= (ImageView)findViewById(R.id.img); 
iv.setImageBitmap(bmp); 

你應該添加清單文件的權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
+0

沒有答案工作:(還嘗試給圖像路徑字符串img _path =「/ storage/sdcard0/Fbt/xylo.jpg」;仍然沒有工作! – Metalhead1247

+0

你有沒有爲它添加權限? – Piyush

+0

哪個特權? – Metalhead1247

1
Image paths stored in database with paths without any extensions 

您需要提及的路徑圖像類型。即image.png

你可以嘗試像下面的例子。

String imagePath = "your image path with image type"; //eg. /sdcard/img.PNG  
    Bitmap bitmap = BitmapFactory.decodeFile(imagePath); 
    ImageView myImageView = (ImageView)findViewById(R.id.imageview);// your imageview 
    myImageView.setImageBitmap(bitmap); 
2

this處取一個雄g。

您可以使用ImageView.setImageURI(..)方法。

preview.setImageURI(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Echo/Images/"+file_name));