2015-08-28 7 views
7

Ich versuche, Bild von SD-Karte in Bitmap für die Anzeige in Bild-Ansicht.Nach dem Lauf zu bekommen die Anwendung zuerst zwei bis drei Bilder angezeigt werden, aber wenn ich Scroll-Liste Anwendung wird abgestürzt und erhalten Ausnahme von NullPointerException: java.lang.NullPointerException: Versuch, virtuelle Methode 'int android.graphics.Bitmap.getWidth()' auf einem Null aufrufen Objektreferenz bei com.example.tazeen.classnkk.Activity1 $ MyListAdapter.getView (Activity1.java:357)Android: NullPointerException: Versuch, virtuelle Methode 'int android.graphics.Bitmap.getWidth()' auf einem Nullobjekt Referenz

   if(objElement.endsWith(mp3_Pattern)) 
       { 
        Log.e("Mp3 ", " ends with "); 

        { 
         int scalFactor = fixHeight/h ; 
         newWidth = (int)(w * scalFactor); 
         Log.e("scalFactor "," = " + scalFactor); 
         Log.e("newWidth "," = " + newWidth); 
         Log.e("resizing... ","in if condition"); 
         new_Height = fixHeight ; 
        } 
        else 
        { 
         newWidth = w; 
         new_Height = h; 
        } 


        Uri uri = Uri.fromFile(new File(objElement)); 
        Picasso.with(getContext()).load(uri).resize(newWidth, new_Height).placeholder(R.drawable.img_placeholder).into(imageView , new com.squareup.picasso.Callback() { 
         @Override 
         public void onSuccess() { 
          if (pBar != null) { 
           pBar.setVisibility(View.GONE); 
          } 
         } 
         @Override 
         public void onError() { 

         } 
        }); 
       } 

       holder.linearLayout.addView(imageView); 
       holder.linearLayout.addView(pBar); 

Hier ist informat log Ion

08-28 14:22:14.077 2947-2947/? E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.tazeen.classnkk, PID: 2947 
    java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference 
      at com.example.tazeen.classnkk.AllPosts_Page$MyListAdapter.getView(AllPosts_Page.java:357) 
      at android.widget.AbsListView.obtainView(AbsListView.java:2347) 
      at android.widget.ListView.makeAndAddView(ListView.java:1864) 
      at android.widget.ListView.fillDown(ListView.java:698) 
      at android.widget.ListView.fillGap(ListView.java:662) 
      at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4991) 
      at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3418) 
      at android.widget.AbsListView.onTouchMove(AbsListView.java:3801) 
      at android.widget.AbsListView.onTouchEvent(AbsListView.java:3632) 
      at android.view.View.dispatchTouchEvent(View.java:8471) 
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2399) 
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2092) 
      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405) 
      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106) 

Antwort

2

versuchen dies. in Ihrem if (objElement.endsWith (png_Pattern))

BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
    Bitmap bitmap = BitmapFactory.decodeFile(objElement, options); 
+0

Aber in meinem String objElement: ist bereits SD-Kartenpfad wie objElement: = /mnt/sdcard/SDImages/95_20150824112324025.png. – androidTag

+0

Ich versuchte oben Code, aber BitmapSize ist Null warum – androidTag

+0

Ich habe die Antwort aktualisiert auch beziehen http://StackOverflow.com/A/8710690/1552136. falls dies nicht gelöst wird, lass es mich wissen, dass ich das versuchen werde. – harshitpthk

1

Versuchen Bitmap bitmapSize = BitmapFactory.decodeFile(objElement); mit

File file = new File(objElement); 
    if (file.exists()) { 
    Bitmap bitmapSize = BitmapFactory.decodeFile(file.getAbsolutePath()); 
    } 
+0

Und warum verwenden Sie 'neue Datei (objElement) .getAbsolutePath()'? Warum nicht 'file.getAbsolutePath()'? – CoolMind

0

ersetzen Es scheint, dass Sie Bilder haben, die nicht decodiert werden konnte.

Aus Dokumentation:

/** 
* Decode a file path into a bitmap. If the specified file name is null, 
* or cannot be decoded into a bitmap, the function returns null. 
* 
* @param pathName complete path name for the file to be decoded. 
* @return the resulting decoded bitmap, or null if it could not be decoded. 
*/ 
public static Bitmap decodeFile(String pathName) { 
    return decodeFile(pathName, null); 
} 
0

I i für diese Antwort wenig zu spät wissen ist. Aber heute stand ich vor demselben Problem. Das Bild mit Bitmap im Picasso zu setzen, hat mein Problem nicht behoben. Ich habe es versucht.

Picasso.with(holder.mRootView.getContext()).load(new File(trackingNumbers.get(position).getImageUrl())).error(R.drawable.no_image_place_holder).noFade().placeholder(R.drawable.no_image_place_holder).into(circularImageView); 

Danke

Verwandte Themen