[英]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*
我的應用程序有1個活動,這應該從galery影像\\照片,保存uri
的這個圖片\\照片sharedPreferences
,並重裝后,應用程序加載圖像從保存的uri
中的ImageView
因此,重新加載uri
成功保存並加載到應用程序后,但應用程序無法加載來自uri
照片,而是圖像我有空的空間,但ImageView
存在並且加載了uri
一切正常(我比較加載的uri
和uri
來自galery,他們等於)。
下面我附上我的代碼和截圖:
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)從@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
檢查我的Android和版本的版本@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
- 它們等於,一切正常
5)嘗試從content://media/external/images/media/16493
更改uri
content://media/external/images/media/16493
到media/external/images/media/16493
- 沒有幫助
6)嘗試使用onRestoreInstanceState
和onSaveInstanceState
- 它沒有幫助, 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>
嘗試首先將imageView設置為null,然后像這樣正常設置:
imageView.setImageURI(null);
imageView.setImageURI(Uri.parse(savedURI));
我嘗試打開流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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.