【问题标题】:select, crop, and save the uri an image in shared preferences在共享首选项中选择、裁剪和保存 uri 图像
【发布时间】:2018-06-30 19:59:54
【问题描述】:

此代码允许用户在他的图库中选择一张图片并对其进行裁剪以将其放入 imageview 中。但是每次我离开应用程序并再次打开它时,图像不再存在,所以我想将用户在共享偏好中裁剪的图像保存为 uri,以便它是持久的。

请喜欢实现 cele!

选择图片:

permissionsCheck();
    Intent intent = new Intent();

    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);

    startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);

裁剪图像:

private void doCrop() {
    final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setType("image/*");

    List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );

    int size = list.size();
    if (size == 0) {
        Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();

        return;
    } else {
        intent.setData(mImageCaptureUri);

        intent.putExtra("outputX", 100);
        intent.putExtra("outputY", 100);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", false);
        intent.putExtra("return-data", true);

        if (size == 1) {
            Intent i        = new Intent(intent);
            ResolveInfo res = list.get(0);
            i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            startActivityForResult(i, CROP_FROM_CAMERA);
        } else {
            for (ResolveInfo res : list) {
                final CropOption co = new CropOption();
                co.title    = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
                co.icon     = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
                co.appIntent= new Intent(intent);
                co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
                cropOptions.add(co);
            }

            CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Choose Crop App");
            builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
                public void onClick( DialogInterface dialog, int item ) {
                    startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
                }
            });

            builder.setOnCancelListener( new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel( DialogInterface dialog ) {

                    if (mImageCaptureUri != null ) {
                        getContentResolver().delete(mImageCaptureUri, null, null );
                        mImageCaptureUri = null;
                    }
                }
            } );

            AlertDialog alert = builder.create();

            alert.show();
        }
    }
}

活动结果:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK) return;

    switch (requestCode) {
        case PICK_IMAGE:
            mImageCaptureUri = data.getData();
            doCrop();
            break;
        case CROP_FROM_CAMERA:
            Bundle extras = data.getExtras();
            if (extras != null) {
                Bitmap photo = extras.getParcelable("data");
                imageView.setImageBitmap(photo);
                String root = Environment.getExternalStorageDirectory().toString();
                File myDir = new File(root + "/saved_images");
                myDir.mkdirs();
                Random generator = new Random();
                int n = 100000;
                n = generator.nextInt(n);
                String fname = "Image-"+ n +".jpg";
                File file = new File (myDir, fname);
                if (file.exists ()) file.delete ();
                try {
                    FileOutputStream out = new FileOutputStream(file);
                    photo.compress(Bitmap.CompressFormat.JPEG, 90, out);
                    out.flush();
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("image", String.valueOf(mImageCaptureUri));
                editor.commit();

                imageView.setImageURI(mImageCaptureUri);
                imageView.invalidate();
            }
            File f = new File(mImageCaptureUri.getPath());
            if (f.exists()) f.delete();
            break;

    }
}

【问题讨论】:

    标签: android sharedpreferences


    【解决方案1】:

    请不要将图像保存到Preferences,它不是存储大块数据的地方,请将裁剪图像以 .jpg 或 .png 图像保存到您的数据文件夹中并将 裁剪后的图像文件 的 URI 或路径存储在您的首选项中。

    然后您将能够检查路径是否存储在首选项中并在应用程序启动时加载裁剪图像。


    好的,我再次检查了您的源代码。在onActivityResult() 中有一个变量mImageCaptureUri——将这个变量保存到Preferences。当您重新启动应用程序时,使用该变量访问图像并再次以与裁剪原始图像相同的方式对其进行裁剪。

    【讨论】:

    • 是的,我可以存储用户选择的任何图像的 uri,但我现在想保存裁剪图像的 uri
    • @Anubis 将裁剪后的文件保存到您的应用目录Environment.getDataDirectory(),然后将文件名存储到首选项中,或者如果图像太多,则使用外部存储器,getExternalStorageDirectory ()
    • @Anubis - 我把你的问题重读了 3 遍。它没有任何地方表明您在保存 uri 而不是图像时遇到了麻烦。 Lenik 像其他人一样理解你的问题。
    • @Anubis 我对答案做了一些更改,可能这会澄清一些事情。
    • @Eugene Kartoyev 我可能表达得不好,但实际上我想保存裁剪图像的uri
    猜你喜欢
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多