2013-05-29 38 views
51

我試圖從現有文件路徑創建一個位圖或Drawable。創建一個位圖/從文件路徑繪製

String path = intent.getStringExtra("FilePath"); 
BitmapFactory.Options option = new BitmapFactory.Options(); 
option.inPreferredConfig = Bitmap.Config.ARGB_8888; 

mImg.setImageBitmap(BitmapFactory.decodeFile(path)); 
// mImg.setImageBitmap(BitmapFactory.decodeFile(path, option)); 
// mImg.setImageDrawable(Drawable.createFromPath(path)); 
mImg.setVisibility(View.VISIBLE); 
mText.setText(path); 

setImageBitmap()setImageDrawable()不顯示從路徑的圖像。我用mText打印路徑,它看起來像:/storage/sdcard0/DCIM/100LGDSC/CAM00001.jpg

我在做什麼錯了?任何人都可以幫助我?

+0

BitmapFactory.decodeFile(路徑) - >這是否返回一個位圖對象嗎?你能證實嗎? – toantran

+1

@ autobot_101在調試模式下,它在'mBuffer'中有'id'。但是它的'mHeight','mWidth'值是'-1','mLayoutBounds'是'null'。 –

+0

然後你應該再次檢查你的文件路徑,因爲這意味着你的圖像沒有被「膨脹」到位圖對象。也許你可以嘗試其他圖像 – toantran

回答

82

從文件創建路徑位圖。

我認爲你給錯了文件路徑。 :) 希望這可以幫助。

+1

很久以前,我已經獲得了我的解決方案,但我將此作爲正確答案,因爲它也可以處理OOM錯誤,重新加載大規模的圖像。非常乾淨漂亮的解決方案!謝謝! –

+1

我假設這裏的imageName是任何字符串?或者它是任何特定的imageName? – Jeet

+0

@JeetendraChoudhary是imageName可以是任何最終的String作爲圖像的名稱。 – CodeShadow

48

它爲我的作品:

File imgFile = new File("/sdcard/Images/test_image.jpg"); 
if(imgFile.exists()){ 
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 
    //Drawable d = new BitmapDrawable(getResources(), myBitmap); 
    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest); 
    myImage.setImageBitmap(myBitmap); 

} 

編輯:

如果上面的硬編碼的SD卡目錄是不是在你的情況下工作,可以獲取SD卡路徑:

String sdcardPath = Environment.getExternalStorageDirectory().toString(); 
File imgFile = new File(sdcardPath); 
+3

嘗試從'Environment.getExternalStorageDirectory()。toString()'獲取SdCard路徑,然後嘗試 – Antarix

0

你不能通過路徑訪問你的drawables,所以如果你想要一個人工可讀的界面,你可以通過編程方式構建你的drawables。

在類的某處聲明一個HashMap:

private static HashMap<String, Integer> images = null; 

//Then initialize it in your constructor: 

public myClass() { 
    if (images == null) { 
    images = new HashMap<String, Integer>(); 
    images.put("Human1Arm", R.drawable.human_one_arm); 
    // for all your images - don't worry, this is really fast and will only happen once 
    } 
} 

現在訪問 -

String drawable = "wrench"; 
// fill in this value however you want, but in the end you want Human1Arm etc 
// access is fast and easy: 
Bitmap wrench = BitmapFactory.decodeResource(getResources(), images.get(drawable)); 
canvas.drawColor(Color .BLACK); 
Log.d("OLOLOLO",Integer.toString(wrench.getHeight())); 
canvas.drawBitmap(wrench, left, top, null); 
3

好,使用靜態Drawable.createFromPath(String pathName)似乎更直接給我一點比你解碼它... :-)

如果您的mImg是一個簡單的ImageView,您甚至不需要它,直接使用mImg.setImageUri(Uri uri) 。如果你想擴展位圖到父的高度和寬度,然後使用Bitmap.createScaledBitmap功能

File sd = Environment.getExternalStorageDirectory(); 
File image = new File(sd+filePath, imageName); 
BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions); 
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true); 
imageView.setImageBitmap(bitmap); 

1
static ArrayList< Drawable> d; 
d = new ArrayList<Drawable>(); 
for(int i=0;i<MainActivity.FilePathStrings1.size();i++) { 
    myDrawable = Drawable.createFromPath(MainActivity.FilePathStrings1.get(i)); 
    d.add(myDrawable); 
} 
+3

請注意,您應該解釋您在答案中提供的代碼。 –

18

這裏是一個解決方案:

Bitmap bitmap = BitmapFactory.decodeFile(filePath);