简体   繁体   English

从图库中选择并通过意图裁剪后,如何将图像存储在应用程序内部存储器中

[英]How to store image in app internal memory, after selecting from gallery and cropping through intent

In my app I am selecting an image from gallery, than I cropped that image, than store it in external storage. 在我的应用程序中,我从图库中选择一个图像,而不是裁剪该图像,而不是将其存储在外部存储中。

But if sd-card not available than problem occurs. 但是,如果无法使用SD卡,则会出现问题。

So here I am asking you how to store image in application internal storage. 因此,我在这里问您如何将图像存储在应用程序内部存储中。

Here is my code for external storage. 这是我的外部存储代码。

Override
    public void onClick(View v) {
        int outputX = 400;
        int outputY = 400;
        int aspectX = 1;
        int aspectY = 1;
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        intent.setType("image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", aspectX);
        intent.putExtra("aspectY", aspectY);
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", false);
        selectedImageUri = getTempUri();
        intent.putExtra(MediaStore.EXTRA_OUTPUT, selectedImageUri);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true);

        startActivityForResult(intent, 2);
    }

Here is how I get URI of externel file: 这是我获取外部文件URI的方法:

private Uri getTempUri() {
        return Uri.fromFile(getTempFile());
    }

    private File getTempFile() {
        if (isSDCARDMounted()) {

            File f = new File(Environment.getExternalStorageDirectory(),
                    TEMP_PHOTO_FILE);
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Toast.makeText(MainActivity.this, "file issue", Toast.LENGTH_LONG)
                        .show();
            }
            return f;
        } else {
            return null;
        }

    }

    private boolean isSDCARDMounted() {
        String status = Environment.getExternalStorageState();

        if (status.equals(Environment.MEDIA_MOUNTED))
            return true;
        return false;
    }

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

        if(requestCode == 2) {
            path = selectedImageUri.getPath();
            imageView.setImageBitmap(BitmapFactory
                    .decodeFile(path));
        }
    }

You can use Context's getCacheDir() method to store your image in application cache directory. 您可以使用Context的getCacheDir()方法将图像存储在应用程序缓存目录中。 Use following code to store your image inside cache directory. 使用以下代码将图像存储在缓存目录中。

//this will create a custom directory inside cache directory
File cacheDir = new File(this.getCacheDir(), "Custom_Directory");
if (!cacheDir.exists())
    cacheDir.mkdir();

//this will give you full path of cache directory. You can use this for your file
this.getCacheDir().getAbsolutePath()

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

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