2011-08-02 9 views
4

SDカードの指定されたパスでカメラから切り取った画像を保存する際に問題が発生しています。Android:カメラから切り取った画像を保存する際の問題

コード - ここ>

public class PhotocropActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 
    private static final int SELECT_PICTURE = 1; 
    private static final int PICK_FROM_CAMERA = 2; 
    private Uri muri; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     final CharSequence[] items = {"Capture New Image", "Upload from gallery"}; 
     AlertDialog.Builder builder = new AlertDialog.Builder(PhotocropActivity.this); 
     builder.setTitle("Select Image"); 
     builder.setItems(items, new DialogInterface.OnClickListener() 
     { 
      public void onClick(DialogInterface dialog, int item) 
      { 
       Intent intent = new Intent(); 
       intent.putExtra("crop", "true"); 
       intent.putExtra("aspectX", 730); 
       intent.putExtra("aspectY", 1115); 
       intent.putExtra("outputX", 730); 
       intent.putExtra("outputY", 1115); 
       intent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile()); 
       intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); 
       if(item==0) 
       { 
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); 
        startActivityForResult(intent, PICK_FROM_CAMERA); 
       } 
       else if(item==1) 
       { 
        intent.setAction(Intent.ACTION_PICK); 
        intent.setType("image/*"); 
        startActivityForResult(intent, SELECT_PICTURE); 
       } 
      } 
      private Uri getTempFile() 
      { 
       muri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"Image_" + String.Valueof(System.currentTimeMillis()) + ".jpg")); 
       return muri; 
      } 
     }); 
     final AlertDialog alert = builder.create(); 
     ((Button) findViewById(R.id.button)).setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View view) 
      { 
       alert.show(); 
      } 
     }); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     super.onActivityResult(requestCode, resultCode, data); 

     switch(requestCode) 
     { 
      case PICK_FROM_CAMERA : if (resultCode == RESULT_OK) 
      { 
       String filePath= muri.getPath(); 
       Toast.makeText(this, filePath, Toast.LENGTH_SHORT).show(); 
       Bitmap selectedImage = BitmapFactory.decodeFile(filePath); 
       ImageView image = (ImageView)findViewById(R.id.selectedimage); 
       image.setImageBitmap(selectedImage); 
      } 
      break; 
      case SELECT_PICTURE : if (resultCode == RESULT_OK) 
      { 
        String filePath= muri.getPath(); 
        Toast.makeText(this, filePath, Toast.LENGTH_SHORT).show(); 
        Bitmap selectedImage = BitmapFactory.decodeFile(filePath); 
        ImageView image = (ImageView)findViewById(R.id.selectedimage); 
        image.setImageBitmap(selectedImage); 
      } 
      break; 
      default: 
       break; 
     } 
    } 
} 

私はそれが、それは(文字列filePathに= muri.getPathでNullPointerExceptionが表示されているデバッグ)。 PICK_FROM_CAMERAにあります。 getTempFile()の上にmuriの値が表示されています。 誰でも手伝ってください。コードでエラーを修正してください。大きな問題があります。

+0

誰でも手伝ってください。 – Geetanjali

答えて

2

ねえこの問題を解決しました。そのコードはありますか? -

case PICK_FROM_CAMERA : if (resultCode == RESULT_OK) 
      { 
       ContentValues values = new ContentValues(); 
       values.put(Images.Media.TITLE, "title"); 
       values.put(Images.Media.BUCKET_ID, "test"); 
       values.put(Images.Media.DESCRIPTION, "test Image taken"); 
       values.put(Images.Media.MIME_TYPE, "image/jpeg"); 
       Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); 
       Bitmap photo = (Bitmap) data.getExtras().get("data"); 
       ((ImageView)findViewById(R.id.selectedimage)).setImageBitmap(photo); 
       OutputStream outstream; 
       try { 
         outstream = getContentResolver().openOutputStream(uri); 
         photo.compress(Bitmap.CompressFormat.JPEG,100, outstream); 
         outstream.close(); 
       } catch (FileNotFoundException e) {} 
       catch (IOException e){} 
関連する問題