简体   繁体   中英

Can't load image from uri after reload app

*//Sorry for bad eng, but in stackoverflow.ru I have no answers you my last chance*

I have app with 1 activity, which should take image\\photo from galery, save uri of this image\\photo in sharedPreferences , and after reload, app load image from saved uri in imageView

So, after reload uri successfully saved and load in app, but app can't load photo from uri and instead image I have empty space, but ImageView exist and with loaded uri all ok(I compare loaded uri and uri from galery, they equals).

Below I attach my code and screenshots:

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

That I did for fix it?

1)Check saved and loaded uri with uri from galery - they equals, I did this with logs

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)Try use getSharedPreferences instead sharedPreferences - it's don't help

3)Try use imageView.setImageURI(null) , imageView.invalidate() , imageView.postInvalidate() , before load of image - don't help.

4)Checked version of my Android and version from @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) - they equals, all ok

5)Try change uri from content://media/external/images/media/16493 to media/external/images/media/16493 - didn't help

6)Try use onRestoreInstanceState and onSaveInstanceState - it's don't help and onRestoreInstanceState started only when you change orientation of screen, but not after reload.

Maybe, if u run it yourself or just write similar app(it takes 5-10 min) and this app will work - in compare becomes clear, which mistake I have.


Guys, I find answer, thank for this - greenapps

I try open stream InputStream is = getContentResolver().openInputStream(uri); and get stack trace and found some string

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

So, just write in AndroidManifest

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

try to set imageView to null at first and then set it normally like this:

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

I try open stream InputStream is = getContentResolver().openInputStream(uri); and get stack trace and found some string

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

So, just write in AndroidManifest

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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