java.io.FileNotFoundException:/mnt/sdcard/1358731920220.jpg(没有这样的文件或目录)

时间:2013-01-21 01:38:07

标签: java android file camera uri

public class BeginTakePhotoScene extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_begin_take_photo_scene);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        takephotobtn = new ImageButton(this);
        takephotobtn.setId(888);
        takephotobtn.setBackgroundResource(R.drawable.take_photo_btn);
        takephotobtnparam = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        takephotobtnparam.addRule(RelativeLayout.CENTER_HORIZONTAL);
        takephotobtnparam.topMargin = margin.heightPixels * 9 / 10 - height;
        takephotobtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                mUri = Uri.fromFile(new File(Environment
                        .getExternalStorageDirectory(), String.valueOf(System
                        .currentTimeMillis()) + ".jpg"));
                camera.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
                camera.putExtra("return-data", false);
                startActivityForResult(camera, PICTURE_RESULT);
            }
        });

        choosephotobtn = new ImageButton(this);
        choosephotobtn.setId(889);
        choosephotobtn.setBackgroundResource(R.drawable.choose_photo_btn);
        choosephotobtnparam = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        choosephotobtnparam.addRule(RelativeLayout.CENTER_HORIZONTAL);
        choosephotobtnparam.addRule(RelativeLayout.BELOW, 888);
        choosephotobtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(intent, "Select Picture"),
                        SELECT_PICTURE);
            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                selectedImageUri = data.getData();
                uncroppedimage = getFilePath(selectedImageUri);

                Intent cropIntent = new Intent("com.android.camera.action.CROP");

                cropIntent.setDataAndType(selectedImageUri, "image/*");

                cropIntent.putExtra("crop", "true");

                cropIntent.putExtra("aspectX", 1);
                cropIntent.putExtra("aspectY", 1);

                cropIntent.putExtra("outputX", 128);
                cropIntent.putExtra("outputY", 128);

                File tempFile;
                tempFile = new File(Environment.getExternalStorageDirectory(),
                        String.valueOf(System.currentTimeMillis()) + ".jpg");
                mSavedUri = Uri.fromFile(tempFile);
                cropIntent.putExtra("output", mSavedUri);

                cropIntent.putExtra("return-data", true);

                startActivityForResult(cropIntent, PIC_CROP);

            } else if (requestCode == PICTURE_RESULT) {
                Log.e("mUri", mUri.getPath());
                uncroppedimage = mUri.getPath();

                Intent cropIntent = new Intent("com.android.camera.action.CROP");

                cropIntent.setDataAndType(mUri, "image/*");

                cropIntent.putExtra("crop", "true");

                cropIntent.putExtra("aspectX", 1);
                cropIntent.putExtra("aspectY", 1);

                cropIntent.putExtra("outputX", 128);
                cropIntent.putExtra("outputY", 128);

                File tempFile;
                tempFile = new File(Environment.getExternalStorageDirectory(),
                        String.valueOf(System.currentTimeMillis()) + ".jpg");
                mSavedUri = Uri.fromFile(tempFile);
                cropIntent.putExtra("output", mSavedUri);

                cropIntent.putExtra("return-data", true);

                startActivityForResult(cropIntent, PIC_CROP);
            } else {
                Log.e("mSavedUri", mSavedUri.getPath());
                profileimage.setImageURI(mSavedUri);
                croppedimage = mSavedUri.getPath();

                profileimage.setVisibility(View.VISIBLE);
                confirmbtn.setVisibility(View.VISIBLE);

                takephotobtnparam.topMargin = margin.heightPixels * 9 / 10
                        - height * 2;
            }
        }
    }

    private String getFilePath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = this.managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int colum_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(colum_index);
        } else
            return null;
    }
}

我记录uri.getpath,它返回file:///mnt/sdcard/1358731920220.jpg。这个链接似乎有额外的/。但是,我不确定为什么会这样?

2 个答案:

答案 0 :(得分:2)

cropIntent.putExtra("return-data", true);

更改为

cropIntent.putExtra("return-data", false);

会将裁剪的图像保存到SD卡中。

答案 1 :(得分:0)

如果尚未添加,请在清单文件中添加此权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />