2012-11-16 43 views
1

當我拍攝照片與我的設備,此碼在inputStream =線崩潰與java.lang.NullPointerException在Android上拍照時發生崩潰。爲什麼?

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    Uri uriImage; 
    InputStream inputStream = null; 
    ImageView imvCover = (ImageView)this.findViewById(R.id.imvCover); 
    if ((requestCode == CAPTURE_IMAGE) && resultCode == Activity.RESULT_OK) { 
     uriImage = data.getData(); 
     try { 
      inputStream = getContentResolver().openInputStream(uriImage); 
      Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, null); 
      imvCover.setImageBitmap(bitmap); 
     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     imvCover.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
     imvCover.setAdjustViewBounds(true); 
    } 
} 

任何想法,爲什麼錯誤?

這是我使用打開相機拍照代碼:

Button btnTakePicture = (Button)this.findViewById(R.id.btnTakePicture); 
    btnTakePicture.setOnClickListener(new OnClickListener() { 
     public void onClick(View arg0) { 
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(intent, CAPTURE_IMAGE); 
     } 
    }); 
+0

請告訴我關於崩潰?一些日誌會有所幫助。 –

+0

看起來像uriImage = data.getData();返回null。 data.getData()不正確的調用? –

回答

0

這種做法可能並不適用於所有設備。相反,您可以指定新圖像的保存位置,然後您可以使用該預定義文件路徑處理新圖像。

File tempFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() 
    + "/your_folder"); 
tempFolder.mkdir(); 
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() 
    + "/your_folder", String.valueOf(System.currentTimeMillis()) + ".jpg"); 
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); 
startActivityForResult(intent, TAKE_PICTURE); 
0

試試這個..

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     File out = Environment.getExternalStorageDirectory(); 
    out = new File(out, "newImage.jpg"); 
    i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out)); 
    startActivityForResult(i, 1); 

,這是onActivity結果..

@Override 
protected void onActivityResult(int requestCode, int resultcode, Intent intent) 
{ 

super.onActivityResult(requestCode, resultcode, intent); 
File out = new File(Environment.getExternalStorageDirectory(), "newImage.jpg"); 

     if(!out.exists()) 
     { 
      Log.v("log", "file not found"); 
      Toast.makeText(getBaseContext(), 

      "Error while capturing image", Toast.LENGTH_LONG) 

      .show(); 

     return; 

     } 

     Log.v("log", "file "+out.getAbsolutePath()); 
     File f = new File(out.getAbsolutePath()); 

     try { 

    ExifInterface exif = new ExifInterface(out.getAbsolutePath()); 
    orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); 
    //Toast.makeText(getApplicationContext(), ""+orientation, 1).show(); 
    Log.v("log", "ort is "+orientation); 

    } catch (IOException e) 
    { 
    e.printStackTrace(); 
    } 
Bitmap photo =decodeFile(f,400,400); 
    } 

,這是decodeFile功能...

public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){ 
    try { 
     //Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

     //The new size we want to scale to 
     final int REQUIRED_WIDTH=WIDTH; 
     final int REQUIRED_HIGHT=HIGHT; 
     //Find the correct scale value. It should be the power of 2. 
     int scale=1; 
     while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT) 
      scale*=2; 

     //Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) {} 
    return null; 
}