*//Sorry for bad eng, but in stackoverflow.ru I have no answers you my last chance*
我有1个活动的应用,应该从galery获取图片\照片,保存uri
中此图片\照片的sharedPreferences
,并在重新加载后,应用加载来自已保存{{1}的图片在imageView中
因此,重新加载uri
成功保存并加载到应用中后,但应用无法从uri
加载照片,而是图片我有空格,但uri
存在且加载ImageView
一切正常(我比较加载的uri
和uri
来自galery,它们等于)。
下面我附上我的代码和截图:
XML:
uri
的java:
<?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>
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;
}
}
我做了修复它吗?
1)检查保存并加载//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
与uri
来自galery - 它们等于,我用日志做了
uri
2)尝试使用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
代替getSharedPreferences
- 它没有帮助
3)在加载图片之前,请尝试使用sharedPreferences
,imageView.setImageURI(null)
,imageView.invalidate()
- 不要帮忙。
4)从imageView.postInvalidate()
检查我的Android版本和版本 - 等于,一切正常
5)尝试将@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
从uri
更改为content://media/external/images/media/16493
- 没有帮助
6)尝试使用media/external/images/media/16493
和onRestoreInstanceState
- 它不会帮助,onSaveInstanceState
只有在您更改屏幕方向时启动,但在重新加载后才会启动。
也许,如果你自己运行它或只是写类似的应用程序(需要5-10分钟),这个应用程序将起作用 - 比较变得清晰,我有哪个错误。
伙计们,我找到答案,谢谢你 - greenapps
我尝试开放流onRestoreInstanceState
并获得堆栈跟踪并找到一些字符串
InputStream is = getContentResolver().openInputStream(uri);
所以,只需在AndroidManifest中写一下
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()
答案 0 :(得分:0)
首先尝试将imageView设置为null,然后像这样正常设置:
imageView.setImageURI(null);
imageView.setImageURI(Uri.parse(savedURI));
答案 1 :(得分:0)
我尝试开放流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>