2013-12-21 75 views
0

我一直在尝试不同的事情,我可以将照片保存在使用相机拍摄的SD卡中(而不是意图),但无法从SD卡中拾取同一张照片并将其放置在ImageView的。我总是得到一个空指针异常。显示从SD卡到ImageView的照片

不知道缺少了什么,希望有人能帮助我:

PictureCallback jpegCallback = new PictureCallback() { 
    public void onPictureTaken(byte[] data, Camera camera) { 
     // Save the image JPEG data to the SD card 
     FileOutputStream fos = null; 
     String fileName = ""; 
     try { 
      fileName = "/mnt/sdcard/DCIM/MyPicture.jpg"; 
      fos = new FileOutputStream(fileName); 
      fos.write(data); 

      fos.close(); 
      Toast.makeText(getBaseContext(), "Image saved:" , 
        Toast.LENGTH_LONG).show(); 
     } catch (FileNotFoundException e) { 
      Log.e(TAG, "File Note Found", e); 
      Toast.makeText(getBaseContext(), "Image couldn't be saved.", 
        Toast.LENGTH_LONG).show(); 
     } catch (IOException e) { 
      Log.e(TAG, "IO Exception", e); 
      Toast.makeText(getBaseContext(), "Image couldn't be saved.", 
        Toast.LENGTH_LONG).show(); 
     } 
     Bitmap bitmap = BitmapFactory.decodeFile(fileName); 
     Log.d(TAG, fileName); 
     mImageView.setImageBitmap(bitmap); 
    } 
}; 

我已尝试过:

try { 
     Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() 
       + "/DCIM/MyPhoto.jpg"); 
     mImageView.setImageBitmap(picture); 
    } catch (Exception e) { 
     Log.e("Error reading file", e.toString()); 
    } 
} 

它保存图片,但试图把在去捕捉ImageView中的图像表示“Error reading file”

logcat: enter image description here

DDMS: enter image description here

对不起大家了......头痛我忘了把mImageView =(ImageView的)findViewById(R.id.imageView1);

我是一个灾难:-)

+0

使用文件名如String path = Environment.getExternalStorageDirectory()。toString()+“/ DCIM/MyPicture.jpg”; –

+0

仍然无效 – Chris

+0

将您的logcat粘贴到此处。 –

回答

0

试试这个,

 try { 
      Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/DCIM/MyPhoto.jpg"); 
      Log.v("Path", Environment.getExternalStorageDirectory().getPath()+"/DCIM/MyPhoto.jpg"); 
      mImageView.setImageBitmap(picture); 
     } catch (Exception e) { 
      Log.e("Error reading file", e.toString()); 
     } 

检查您的ImageView mImageView初始化或不

+0

如果您输入了正确的文件路径,请输入“File not found”消息 – Chris

+0

? – SathishKumar

+0

是的:/mnt/sdcard/DCIM/MyPhoto.jpg – Chris

0

使用此。它会帮助你。

public class LoadImagesFromSDCard extends AsyncTask<String, Void, Void> { 

    private ProgressDialog Dialog = new ProgressDialog(CameraPhotoCapture.this); 

    Bitmap mBitmap; 

    protected void onPreExecute() { 
     /****** NOTE: You can call UI Element here. *****/ 

     //UI Element 
     Dialog.setMessage("Loading image from Sdcard.."); 
     Dialog.show(); 
    } 

    // Call after onPreExecute method 
    protected Void doInBackground(String... urls) { 

     Bitmap bitmap = null; 
     Bitmap newBitmap = null; 
     Uri uri = null;  


      try { 

       /** Uri.withAppendedPath Method Description 
       * Parameters 
       * baseUri Uri to append path segment to 
       * pathSegment encoded path segment to append 
       * Returns 
       * a new Uri based on baseUri with the given segment appended to the path 
       */ 

       uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + urls[0]); 

       /************** Decode an input stream into a bitmap. *********/ 
       bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); 

       if (bitmap != null) { 

        /********* Creates a new bitmap, scaled from an existing bitmap. ***********/ 

        newBitmap = Bitmap.createScaledBitmap(bitmap, 170, 170, true); 

        bitmap.recycle(); 

        if (newBitmap != null) { 

         mBitmap = newBitmap; 
        } 
       } 
      } catch (IOException e) { 
       //Error fetching image, try to recover 

       /********* Cancel execution of this task. **********/ 
       cancel(true); 
      } 

     return null; 
    } 

    protected void onPostExecute(Void unused) { 

     // NOTE: You can call UI Element here. 

     // Close progress dialog 
     Dialog.dismiss(); 

     if(mBitmap != null) 
      showImg.setImageBitmap(mBitmap); 
    } 
} 
+0

对不起,我不知道如何申请:-( – Chris