如何存储图像中的应用程序内部存储器,从图库中选择,并通过种植意向后, [英] How to store image in app internal memory, after selecting from gallery and cropping through intent

查看:102
本文介绍了如何存储图像中的应用程序内部存储器,从图库中选择,并通过种植意向后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我从图库中选择图片,比我冒出的形象,比其存储在外部存储。

但是,如果出现SD卡不超过可用的问题。

所以在这里我问你如何存储图像中的应用程序的内部存储。

下面是我的code用于外部存储。

 覆盖
    公共无效的onClick(视图v){
        INT outputX = 400;
        INT outputY = 400;
        INT aspectX = 1;
        INT aspectY = 1;
        意向意图=新意图(Intent.ACTION_GET_CONTENT,NULL);
        intent.setType(图像/ *);
        intent.putExtra(裁剪,真);
        intent.putExtra(aspectX,aspectX);
        intent.putExtra(aspectY,aspectY);
        intent.putExtra(outputX,outputX);
        intent.putExtra(outputY,outputY);
        intent.putExtra(规模化,真正的);
        intent.putExtra(回归数据,FALSE);
        selectedImageUri = getTempUri();
        intent.putExtra(MediaStore.EXTRA_OUTPUT,selectedImageUri);
        intent.putExtra(OUTPUTFORMAT,Bitmap.Com pressFormat.JPEG.toString());
        intent.putExtra(noFaceDetection,真正的);        startActivityForResult(意向,2);
    }

下面是我如何得到externel文件的URI:

 私人乌里getTempUri(){
        返回Uri.fromFile(getTempFile());
    }    私人文件getTempFile(){
        如果(isSDCARDMounted()){            文件f =新的文件(Environment.getExternalStorageDirectory()
                    TEMP_PHOTO_FILE);
            尝试{
                f.createNewFile();
            }赶上(IOException异常五){
                // TODO自动生成catch块
                Toast.makeText(MainActivity.this文件的问题,Toast.LENGTH_LONG)
                        。显示();
            }
            返回F;
        }其他{
            返回null;
        }    }    私人布尔isSDCARDMounted(){
        字符串状态= Environment.getExternalStorageState();        如果(status.equals(Environment.MEDIA_MOUNTED))
            返回true;
        返回false;
    }    @覆盖
    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        super.onActivityResult(要求code,结果code,数据);        如果(要求code == 2){
            PATH = selectedImageUri.getPath();
            imageView.setImageBitmap(BitmapFactory
                    由Matchi.com提供回到codeFILE(路径));
        }
    }


解决方案

您可以使用上下文的getCacheDir() 方式来存储应用程序缓存目录中的形象。使用以下code将图像存储缓存目录中。

  //这将创建高速缓存目录中的自定义目录
文件cacheDir =新的文件(this.getCacheDir(),Custom_Directory);
如果(!cacheDir.exists())
    cacheDir.mkdir();//这会给你的缓存目录的完整路径。您可以使用此为您的文件
this.getCacheDir()。getAbsolutePath()

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.

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:

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. 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()

这篇关于如何存储图像中的应用程序内部存储器,从图库中选择,并通过种植意向后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆