1

デバイスで写真を撮ると、このコードは次inputStream =のエラーでクラッシュします。java.lang.NullPointerException

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);
        }
    });
4

2 に答える 2

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);
于 2012-11-17T07:03:19.460 に答える
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;
 }
于 2012-11-17T07:08:46.440 に答える