2013-05-29 10 views
51

Sto provando a creare un bitmap o un disegno dal percorso file esistente.Crea un bitmap/disegnabile dal percorso file

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

Ma setImageBitmap(), setImageDrawable() non mostra un'immagine dal percorso. Ho stampato il percorso con mText e sembra: /storage/sdcard0/DCIM/100LGDSC/CAM00001.jpg

Cosa sto facendo di sbagliato? Qualcuno può aiutarmi?

+0

BitmapFactory.decodeFile (percorso) -> fa questo restituisce un oggetto Bitmap per te? puoi verificarlo? – toantran

+1

@ autobot_101 in modalità debug, ha 'id' in' mBuffer'. Ma il suo valore 'mHeight',' mWidth' è '-1', e' mLayoutBounds' è 'null'. –

+0

Quindi dovresti ricontrollare il percorso del file, poiché ciò significa che l'immagine non è stata "gonfiata" sull'oggetto bitmap. Forse puoi provare un'altra immagine – toantran

risposta

82

Crea bitmap dal percorso del file:

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

Se si desidera ridimensionare la bitmap all'altezza del genitore e la larghezza quindi utilizzare la funzione Bitmap.createScaledBitmap.

Penso che stai dando il percorso del file sbagliato. :) Spero che questo ti aiuti.

+1

Ho già ottenuto la mia soluzione molto tempo fa, ma prenderò come risposta corretta perché può anche gestire gli errori OOM mentre si ' caricamento di un'immagine su grande scala. Soluzione molto pulita e piacevole! Grazie! –

+1

Immagino che imageName qui sia una stringa qualsiasi? o è un nome immagine specifico? – Jeet

+0

@JeetendraChoudhary Sì imageName potrebbe essere una stringa finale come il nome dell'immagine. – CodeShadow

48

funziona per me:

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

} 

Edit:

Se sopra directory sdcard hard-coded non funziona nel tuo caso, è possibile recuperare il percorso sdcard:

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

Provare a ottenere il percorso SdCard da 'Environment.getExternalStorageDirectory(). ToString()' e quindi provare – Antarix

0

non è possibile accedere ai file eseguibili tramite un percorso, quindi se si desidera un'interfaccia leggibile con i propri drawable che è possibile creare in modo programmatico.

dichiarano un HashMap da qualche parte nella tua classe:

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

Ora, per l'accesso -

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

Ebbene, utilizzando la statica Drawable.createFromPath(String pathName) sembra un po 'più semplice per me di decodifica da soli ... :-)

Se il tuo mImg è un semplice ImageView, non ne hai nemmeno bisogno, usa mImg.setImageUri(Uri uri) direttamente .

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

Si noti che è necessario spiegare il codice fornito in risposta. –

18

Ecco una soluzione:

Bitmap bitmap = BitmapFactory.decodeFile(filePath);