无法从图库加载图片

时间:2016-06-12 08:17:57

标签: java android numberformatexception

当我打开我的画廊以便在节目中选择我的照片时。我得到以下错误。它返回null。 这是我的幻灯片扩展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;
   }

在另一个SlideshowEditor中扩展了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  
   }

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

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

我的logcat:

  

执行doInBackground()时发生错误           在android.os.AsyncTask $ 3.done(AsyncTask.java:300)           在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)           at java.util.concurrent.FutureTask.setException(FutureTask.java:222)           在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)引起:java.lang.NumberFormatException:无效的int:“image:54305”           在java.lang.Integer.invalidInt(Integer.java:137)           在java.lang.Integer.parse(Integer.java:374)           在java.lang.Integer.parseInt(Integer.java:365)           在java.lang.Integer.parseInt(Integer.java:331)           在com.harshadjadav.slideshow.Slideshow.getThumbnail(Slideshow.java:328)           在com.harshadjadav.slideshow.SlideshowEditor $ LoadThumbnailTask​​.doInBackground(SlideshowEditor.java:225)           在com.harshadjadav.slideshow.SlideshowEditor $ LoadThumbnailTask​​.doInBackground(SlideshowEditor.java:215)           在android.os.AsyncTask $ 2.call(AsyncTask.java:288)           在java.util.concurrent.FutureTask.run(FutureTask.java:237)   在

     

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

1 个答案:

答案 0 :(得分:0)

getThumbnail()方法中,Uri.getLastPathSegment()将返回String,因此您收到此错误,您正在尝试将String对象解析为{{1} }

您可以将方法更新为

int

可以像

一样打电话
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; }