【问题标题】:Load image from gallery to ImageView with SharedPreferences使用 SharedPreferences 将图像从画廊加载到 ImageView
【发布时间】:2017-12-19 17:18:08
【问题描述】:

我希望你能帮助我。这是我第一次在这里写问题。

我想从图库中选择一张图片并将其显示在 ImageView 中。关闭该活动并重新打开它后,我想显示我们之前选择的图像。选择 Image -> Show Image in ImageView -> Save Image Selection -> Close Activity -> Open Activity -> Show Image Selection。 希望我已经足够清楚了。

从图库中选择图像并将其加载到 ImageView 的第一部分正在工作,目前给我的问题是我正在尝试将所选图像或其路径保存在 SharedPreferences/Preferences 中,但在打开再次活动。使用 SharedPreferences/Preferences 进行保存和加载似乎可行,但它不会显示图片。

我尝试过:Path、EncodedPath 等,但似乎没有任何效果。 我做错了什么?

打开画廊:

    Intent gallery = new Intent(Intent.ACTION_GET_CONTENT);
    gallery.setType("image/*");
    startActivityForResult(gallery, RESULT_LOAD_IMAGE);

保存图片:

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

    if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
        Uri imageUri = data.getData();
        imageView.setImageURI(imageUri);

        SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("imageURI", imageUri.getEncodedPath());
        editor.commit();
    }
}

用于加载所选图像:

        SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
    String imageUriString = settings.getString("imageURI", "");
    Uri imageUri = Uri.parse(imageUriString);

    Picasso.with(this)
            .load(imageUriString)
            //.load(testarrraylogo.get(position))
            .placeholder(R.drawable.otto)
            .error(R.drawable.hochbahn)
            // To fit image into imageView
            .fit()
            // To prevent fade animation
            .noFade()
            .into(imageView);

【问题讨论】:

    标签: android image sharedpreferences android-preferences android-gallery


    【解决方案1】:
    SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("imageURI", imageUri.getEncodedPath());
    editor.commit();
    

    进入

    SharedPreferences.Editor editor = getSharedPreferences("preference", 
    MODE_PRIVATE).edit();
    editor.putString("imageURI", "uriPathstring");  //Your URI PATH STRING
    editor.apply();
    

    然后像这样检索

    SharedPreferences prefs = getSharedPreferences("preference, MODE_PRIVATE); 
    String imageUri = prefs.getString("imageURI", null);
    

    在毕加索中加载图像

    Picasso.with(context).load(new File(imageuri)).into(imageView);
    

    【讨论】:

    • 这永远不会起作用 editor.putString("imageURI", "");您将始终保存一个空字符串
    • 不幸的是,这似乎不起作用。我完全使用了您发布的陈述,但最后我只得到一张空白图片。请参阅上面的@cherif 答案,我说过为毕加索使用占位符和错误图像会返回该错误图像。我做错了什么,尝试了很多不同的方法
    • 我使用了几种不同的代码变体,但其中一种在打开 Activity 时导致应用程序崩溃。您建议的代码“Picasso.with(context).load(new File(imageuri)).into(imageView);”只返回一个空的空白图像 Picasso.with(GlossarDetailsActivity.this).load(new File(imageUri)).error(R.drawable.XYZ).into(imageView);结果在 R.drawable.XYZ 中显示该图像
    【解决方案2】:

    第一次检索图像时您没有使用 Picasso 加载图像,您可以将这个imageView.setImageURI(imageUri); 替换为这个Picasso.with(YourActivity.this).load(imageData).into(imageView)

    然后要将 Uri 保存到 SharedPreferences,您可以执行类似 editor.putString("imageURI", imageUri.toString); 的操作,然后使用

    检索它
    try {
       imageUri = URI.create(myPrefs.getString("imageURI", "defaultString"));
    } catch (IllegalArgumentException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }
    

    【讨论】:

    • 正如我在问题部分中提到的,即使他正在检索字符串/URI,图片也没有显示。 Uri String 看起来像这样:“content://com.android.providers.media.documents/document/image%3A38666”当我在 Picasso 中使用占位符和错误时,它首先会在很短的时间内显示占位符图像,然后它显示错误图像。在调试时,我可以看到从 SharedPreferences 保存和检索是有效的,但如前所述,它在选择它时显示选定的图像,但当我想在从 Preferences 检索它后显示该图像时不显示该图像
    猜你喜欢
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    相关资源
    最近更新 更多