简体   繁体   中英

unable to load image from gallery

When I open my gallery in order to select my photo in show. I got this following errors.and It returns null. this my slideshow that extends ListActivity :

  public static Bitmap getThumbnail(Uri uri, ContentResolver cr, 
      BitmapFactory.Options options)
     {
        int id = Integer.parseInt(uri.getLastPathSegment());

      Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(cr, id, 
         MediaStore.Images.Thumbnails.MICRO_KIND, options);         

      return bitmap;
   }

and here in another SlideshowEditor extends ListActivity:

    private class LoadThumbnailTask extends AsyncTask<Object,Object,Bitmap>
       {
  ImageView imageView; // displays the thumbnail

  // load thumbnail: ImageView, MediaType and Uri as args
  @Override
  protected Bitmap doInBackground(Object... params)
  {
     imageView = (ImageView) params[0];

     return Slideshow.getThumbnail((Uri) params[1], 
        getContentResolver(), new BitmapFactory.Options());
  } // end method doInBackground

  // set thumbnail on ListView
  @Override
  protected void onPostExecute(Bitmap result)
  {
     super.onPostExecute(result);
     imageView.setImageBitmap(result);
  } // end method onPostExecute  
   }

and

protected Bitmap doInBackground(Object... params)
  {
     imageView = (ImageView) params[0];

     return Slideshow.getThumbnail((Uri) params[1], 
        getContentResolver(), new BitmapFactory.Options());
  } 

my logcat :

An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:300) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:841) Caused by: java.lang.NumberFormatException: Invalid int: "image:54305" at java.lang.Integer.invalidInt(Integer.java:137) at java.lang.Integer.parse(Integer.java:374) at java.lang.Integer.parseInt(Integer.java:365) at java.lang.Integer.parseInt(Integer.java:331) at com.harshadjadav.slideshow.Slideshow.getThumbnail(Slideshow.java:328) at com.harshadjadav.slideshow.SlideshowEditor$LoadThumbnailTask.doInBackground(SlideshowEditor.java:225) at com.harshadjadav.slideshow.SlideshowEditor$Load ThumbnailTask.doInBackground(SlideshowEditor.java:215) at android.os.AsyncTask$2.call(AsyncTask.java:288) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at

java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

In your getThumbnail() method, the Uri.getLastPathSegment() will return a String so you are getting this error, you are trying to parse a String object to int

You can update your method as

public static Bitmap getThumbnail(ContentResolver cr, String path) throws Exception { Cursor ca = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.MediaColumns._ID }, MediaStore.MediaColumns.DATA + "=?", new String[] {path}, null); if (ca != null && ca.moveToFirst()) { int id = ca.getInt(ca.getColumnIndex(MediaStore.MediaColumns._ID)); ca.close(); return MediaStore.Images.Thumbnails.getThumbnail(cr, id, MediaStore.Images.Thumbnails.MICRO_KIND, null ); } ca.close(); return null; }

And can call like

getThumbnail(getContentResolver, uri.tostring)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM