【问题标题】:Can't load image from uri after reload app重新加载应用程序后无法从uri加载图像
【发布时间】:2017-05-10 09:48:04
【问题描述】:
*//Sorry for bad eng, but in stackoverflow.ru I have no answers you my last chance*

我有 1 个活动的应用程序,它应该从图库中获取图像\照片,将此图像\照片的 uri 保存在 sharedPreferences 中,重新加载后,应用程序从保存的图像视图中的 uri 加载图像

所以,在重新加载uri 成功保存并加载到应用程序后,应用程序无法从uri 加载照片,而是图像我有空白空间,但ImageView 存在并且加载了uri 一切正常(我比较从画廊加载的uriuri,它们相等)。

下面附上我的代码和截图:

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ua.bellkross.testapp.MainActivity">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@drawable/myphoto2" />
</LinearLayout>

java:

package ua.bellkross.testapp;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    ImageView imageView;
    SharedPreferences sharedPreferences;
    String photoUri;
    boolean can;

    final int GALLERY_REQUEST = 1;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.imageView);
        loadPhoto();
        imageView.setOnClickListener(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        savePhoto();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imageView :
                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Uri selectedImage = data.getData();
        imageView.setImageURI(selectedImage);
        photoUri = selectedImage.toString();
        Log.d("myLog", "savedURI == " + photoUri);
        can = true;
    }

    private void savePhoto(){
        sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("uri",photoUri);
        editor.putBoolean("edited",can);
        editor.commit();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void loadPhoto(){
        sharedPreferences = getPreferences(MODE_PRIVATE);
        if(sharedPreferences.getBoolean("edited",false)) {
            String savedURI = sharedPreferences.getString("uri", null);
            if (!savedURI.equals(null)) {
                Log.d("myLog", "savedURI!=null");
                imageView.setImageURI(Uri.parse(savedURI));
                Log.d("myLog", "savedURI == " + savedURI);
            }
        }
        can = false;
    }
}

//I have no reputation to post images and more than 2 links
    https://i.stack.imgur.com/hveS0.jpg - start app
    https://i.stack.imgur.com/UrzHF.jpg - take image from galery
    https://i.stack.imgur.com/Cwy37.jpg - after reload app

我做了什么来修复它?

1)检查已保存和加载的uri 和来自画廊的uri - 它们相等,我使用日志执行此操作

05-09 22:05:24.893 524-524/ua.bellkross.testapp D/myLog: savedURI(OnActivityResult) == content://media/external/images/media/16493
05-09 22:05:29.505 1294-1294/? D/myLog: savedURI!=null
05-09 22:05:29.510 1294-1294/? D/myLog: savedURI == content://media/external/images/media/16493

2) 尝试使用 getSharedPreferences 代替 sharedPreferences - 没有帮助

3) 尝试在加载图像之前使用imageView.setImageURI(null)imageView.invalidate()imageView.postInvalidate() - 没有帮助。

4) 检查我的 Android 版本和来自 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 的版本 - 它们相等,一切正常

5)尝试将 uricontent://media/external/images/media/16493 更改为 media/external/images/media/16493 - 没有帮助

6) 尝试使用onRestoreInstanceStateonSaveInstanceState - 没有帮助,onRestoreInstanceState 仅在您更改屏幕方向时启动,但在重新加载后不会启动。

也许,如果你自己运行它或只是编写类似的应用程序(需要 5-10 分钟)并且这个应用程序将工作 - 比较清楚,我有哪个错误。


各位,我找到答案了,谢谢 - greenapps

我尝试打开流InputStream is = getContentResolver().openInputStream(uri); 并获取堆栈跟踪并找到一些字符串

05-10 16:13:40.081 1264-1286/? E/DatabaseUtils: Writing exception to parcel
                                                java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/18553 from pid=8465, uid=10268 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

所以,只需在 AndroidManifest 中编写

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>

【问题讨论】:

  • 请做一个测试,尝试为保存的 uri 打开一个 InputStream。 InputStream is = getContentResolver().openInputStream(uri);能否打开请报告。在 onActivityResult 和重启后都这样做。
  • 在 onActivityResult 中一切正常,但是当我执行“InputStream is = getContentResolver().openInputStream(Uri.parse(photoUri));”时在 loadPhoto() 我得到异常
  • 哦,非常感谢!我找到答案我应该写在 Android Manifest 我刚刚阅读了我的堆栈跟踪并找到了答案,我没有法律可以在其中查找图像电话“android.permission.READ_EXTERNAL_STORAGE”
  • 都很奇怪。据我所知,对于内容框架,不需要权限。

标签: java android imageview sharedpreferences


【解决方案1】:

先尝试将imageView设置为null,然后正常设置如下:

imageView.setImageURI(null);
imageView.setImageURI(Uri.parse(savedURI));

【讨论】:

    【解决方案2】:

    我尝试打开流InputStream is = getContentResolver().openInputStream(uri); 并获取堆栈跟踪并找到一些字符串

    05-10 16:13:40.081 1264-1286/? E/DatabaseUtils: Writing exception to parcel
                                                    java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/18553 from pid=8465, uid=10268 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
    

    所以,只要写在 AndroidManifest 中

      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
    

    【讨论】:

    • 显然您为 Android
    • 哈....现在我明白了....一开始似乎是一个新问题...对此感到抱歉... :)..没有看到编辑问题,只是无法在上下文中得到它......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    • 2021-10-19
    • 2021-11-27
    • 1970-01-01
    相关资源
    最近更新 更多