将从库中选择的照片保存到缓存目录

时间:2015-04-14 17:20:02

标签: java android android-intent

我编写了一个在我的应用缓存目录中创建目录的方法,然后提示用户拍照或从库中选择一张照片。

如果用户拍摄新照片,照片会以profile.jpg

的形式保存到缓存中

我试图获取从图库中返回照片的意图,以将返回的照片保存在我的应用的缓存目录中作为profile.jpg。

我无法实现这一目标。

public void selectImage(View v) {

        File newDir = new File(getExternalCacheDir(), "RecruitSwift");
        if(!newDir.isDirectory())
            newDir.mkdirs();
        else
            Toast.makeText(this, "Dir already exist", Toast.LENGTH_LONG).show();

        if(newDir.canWrite())
            imageFile = new File(newDir, "profile.jpg");
        else
            Toast.makeText(this, "Dir not writable", Toast.LENGTH_LONG).show();

        final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };

        AlertDialog.Builder builder = new AlertDialog.Builder(UserProfileInterviewScreenActivity.this);
        builder.setTitle("Add Photo!");
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (options[item].equals("Take Photo")){
                    Intent takeProfileImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if (takeProfileImage.resolveActivity(getPackageManager()) != null) {

                        Uri imageUri = Uri.fromFile(imageFile);
                        takeProfileImage.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                        startActivityForResult(takeProfileImage, 1);
                    }
                }
                else if (options[item].equals("Choose from Gallery")){
                    Intent takeProfileImage = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                    Uri selectedImage = takeProfileImage.getData();
                    imageFile = selectedImage;
                    startActivityForResult(takeProfileImage, 2);

                }
                else if (options[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }

2 个答案:

答案 0 :(得分:1)

正如@greenapps指出的那样,它的自我意图不会返回任何内容,你必须在onActivityResult方法中获取返回的内容提供者路径并将其转换为文件系统路径。

这是我最后写的代码:

Uri selectedImage = data.getData();
                String[] filePath = { MediaStore.Images.Media.DATA };
                Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);
                String picturePath = c.getString(columnIndex);
                c.close();
                Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
                Log.w("path of profileImage.setImageBitmap(thumbnail);
                try{
                    String file_path = getExternalCacheDir() + "/MyWebsiteDir";
                    File dir = new File(file_path);
                    if(!dir.exists())
                        dir.mkdirs();
                    File file = new File(dir, "profile.jpg");
                    FileOutputStream fOut = new FileOutputStream(file);
                    thumbnail.compress(Bitmap.CompressFormat.PNG, 85, fOut);
                    fOut.flush();
                    fOut.close();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }

答案 1 :(得分:1)

如果您想拍照或选择照片,则需要拨打startActivityForResult,然后覆盖onActivityResult。以下是拍摄照片的一些代码,请看一下:


startActivityForResult

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(AVATAR_FILE_TMP));
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, CODE_TAKE_PHOTO);

然后覆盖onActivityResult

if (resultCode == Activity.RESULT_OK) {
        if (requestCode == CODE_TAKE_PHOTO) {
            cropImage(Uri.fromFile(AVATAR_FILE_TMP));
        }
}