回答編集履歴
1
あいういえお
answer
CHANGED
|
@@ -1,3 +1,126 @@
|
|
|
1
1
|
saveボタンを押した時にonActivityResultで取得したUriをtoStringで文字列化して、SharedPrefrenceかSQLite等に保存してはどうでしょうか?
|
|
2
2
|
|
|
3
|
-
画面再起動時は保存した文字列を読み込んでUriに戻しBitmapを表示してはどうですか?
|
|
3
|
+
画面再起動時は保存した文字列を読み込んでUriに戻しBitmapを表示してはどうですか?
|
|
4
|
+
|
|
5
|
+
```java
|
|
6
|
+
public class MainActivity extends AppCompatActivity {
|
|
7
|
+
private static final int RESULT_PICK_IMAGEFILE = 1001;
|
|
8
|
+
private static final String KEY = "key";
|
|
9
|
+
private ImageView imageView;
|
|
10
|
+
private String url = null;
|
|
11
|
+
|
|
12
|
+
@Override
|
|
13
|
+
protected void onCreate(Bundle savedInstanceState) {
|
|
14
|
+
super.onCreate(savedInstanceState);
|
|
15
|
+
setContentView(R.layout.activity_main);
|
|
16
|
+
imageView = findViewById(R.id.imageEdit);
|
|
17
|
+
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
|
|
18
|
+
String lastUrl = sp.getString(KEY, null);
|
|
19
|
+
if(lastUrl != null) {
|
|
20
|
+
loadImage(Uri.parse(lastUrl));
|
|
21
|
+
}
|
|
22
|
+
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
|
|
23
|
+
public void onClick(View v) {
|
|
24
|
+
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
|
25
|
+
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
|
26
|
+
intent.setType("*/*");
|
|
27
|
+
startActivityForResult(intent, RESULT_PICK_IMAGEFILE);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
|
|
32
|
+
public void onClick(View v) {
|
|
33
|
+
if(url != null) {
|
|
34
|
+
sp.edit().putString(KEY,url).apply();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
|
41
|
+
@Override
|
|
42
|
+
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
|
|
43
|
+
super.onActivityResult(requestCode, resultCode, resultData);
|
|
44
|
+
if (requestCode == RESULT_PICK_IMAGEFILE && resultCode == Activity.RESULT_OK && resultData.getData() != null) {
|
|
45
|
+
Uri uri = resultData.getData();
|
|
46
|
+
final int takeFlags = resultData.getFlags()
|
|
47
|
+
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
|
|
48
|
+
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
|
|
49
|
+
try {
|
|
50
|
+
getContentResolver().takePersistableUriPermission(uri, takeFlags);
|
|
51
|
+
}
|
|
52
|
+
catch (SecurityException e){
|
|
53
|
+
e.printStackTrace();
|
|
54
|
+
}
|
|
55
|
+
url = uri.toString();
|
|
56
|
+
loadImage(uri);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private void loadImage(Uri uri) {
|
|
61
|
+
ParcelFileDescriptor pfDescriptor = null;
|
|
62
|
+
try {
|
|
63
|
+
pfDescriptor = getContentResolver().openFileDescriptor(uri, "r");
|
|
64
|
+
if (pfDescriptor != null) {
|
|
65
|
+
FileDescriptor fileDescriptor = pfDescriptor.getFileDescriptor();
|
|
66
|
+
Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor);
|
|
67
|
+
pfDescriptor.close();
|
|
68
|
+
imageView.setImageBitmap(bmp);
|
|
69
|
+
}
|
|
70
|
+
} catch (IOException e) {
|
|
71
|
+
e.printStackTrace();
|
|
72
|
+
} finally {
|
|
73
|
+
try {
|
|
74
|
+
if (pfDescriptor != null) {
|
|
75
|
+
pfDescriptor.close();
|
|
76
|
+
}
|
|
77
|
+
} catch (Exception e) {
|
|
78
|
+
e.printStackTrace();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
```xml
|
|
86
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
87
|
+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
88
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
|
89
|
+
xmlns:tools="http://schemas.android.com/tools"
|
|
90
|
+
android:layout_width="match_parent"
|
|
91
|
+
android:layout_height="match_parent"
|
|
92
|
+
tools:context=".MainActivity">
|
|
93
|
+
|
|
94
|
+
<ImageView
|
|
95
|
+
android:id="@+id/imageEdit"
|
|
96
|
+
android:layout_width="match_parent"
|
|
97
|
+
android:layout_height="300dp"
|
|
98
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
|
99
|
+
app:layout_constraintEnd_toEndOf="parent"
|
|
100
|
+
app:layout_constraintHorizontal_bias="0.498"
|
|
101
|
+
app:layout_constraintStart_toStartOf="parent"
|
|
102
|
+
app:layout_constraintTop_toTopOf="parent"
|
|
103
|
+
app:layout_constraintVertical_bias="0.476" />
|
|
104
|
+
|
|
105
|
+
<Button
|
|
106
|
+
android:id="@+id/button"
|
|
107
|
+
android:layout_width="wrap_content"
|
|
108
|
+
android:layout_height="wrap_content"
|
|
109
|
+
android:text="load"
|
|
110
|
+
app:layout_constraintBottom_toBottomOf="@id/save"
|
|
111
|
+
app:layout_constraintEnd_toEndOf="parent"
|
|
112
|
+
app:layout_constraintStart_toStartOf="parent"
|
|
113
|
+
app:layout_constraintTop_toBottomOf="@+id/imageEdit" />
|
|
114
|
+
|
|
115
|
+
<Button
|
|
116
|
+
android:id="@+id/save"
|
|
117
|
+
android:layout_width="wrap_content"
|
|
118
|
+
android:layout_height="wrap_content"
|
|
119
|
+
android:text="save"
|
|
120
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
|
121
|
+
app:layout_constraintEnd_toEndOf="parent"
|
|
122
|
+
app:layout_constraintStart_toStartOf="parent"
|
|
123
|
+
app:layout_constraintTop_toBottomOf="@+id/button" />
|
|
124
|
+
|
|
125
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
126
|
+
```
|