简体   繁体   English

Android:问题是保存从相机拍摄的裁剪图像

[英]Android:Problem in saving cropped image taken from camera

I am getting problem in saving a cropped image taken from camera at specified path in SD card. 我在将从相机拍摄的裁剪图像保存到SD卡中的指定路径时遇到问题。

code--> 代码->

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

here when I debug it then it is showing NULLPOINTEREXCEPTION at String filePath= muri.getPath(); 在这里,当我调试它时,它在字符串filePath = muri.getPath();中显示NULLPOINTEREXCEPTION。 in PICK_FROM_CAMERA. 在PICK_FROM_CAMERA中。 but it's showing muri value above in getTempFile(). 但它在getTempFile()中显示了上面的muri值。 Anyone help me.Fix the error in code?I m in great trouble. 有人帮我解决代码中的错误吗?

hey I have solved this problem. 嘿,我已经解决了这个问题。 and its perfect.here is the code--> 及其完美。代码在这里->

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){}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM