回答編集履歴
1
あいういえお
test
CHANGED
@@ -3,3 +3,249 @@
|
|
3
3
|
|
4
4
|
|
5
5
|
画面再起動時は保存した文字列を読み込んでUriに戻しBitmapを表示してはどうですか?
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
```java
|
10
|
+
|
11
|
+
public class MainActivity extends AppCompatActivity {
|
12
|
+
|
13
|
+
private static final int RESULT_PICK_IMAGEFILE = 1001;
|
14
|
+
|
15
|
+
private static final String KEY = "key";
|
16
|
+
|
17
|
+
private ImageView imageView;
|
18
|
+
|
19
|
+
private String url = null;
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
@Override
|
24
|
+
|
25
|
+
protected void onCreate(Bundle savedInstanceState) {
|
26
|
+
|
27
|
+
super.onCreate(savedInstanceState);
|
28
|
+
|
29
|
+
setContentView(R.layout.activity_main);
|
30
|
+
|
31
|
+
imageView = findViewById(R.id.imageEdit);
|
32
|
+
|
33
|
+
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
|
34
|
+
|
35
|
+
String lastUrl = sp.getString(KEY, null);
|
36
|
+
|
37
|
+
if(lastUrl != null) {
|
38
|
+
|
39
|
+
loadImage(Uri.parse(lastUrl));
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
|
44
|
+
|
45
|
+
public void onClick(View v) {
|
46
|
+
|
47
|
+
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
48
|
+
|
49
|
+
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
50
|
+
|
51
|
+
intent.setType("*/*");
|
52
|
+
|
53
|
+
startActivityForResult(intent, RESULT_PICK_IMAGEFILE);
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
});
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
|
62
|
+
|
63
|
+
public void onClick(View v) {
|
64
|
+
|
65
|
+
if(url != null) {
|
66
|
+
|
67
|
+
sp.edit().putString(KEY,url).apply();
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
});
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
80
|
+
|
81
|
+
@Override
|
82
|
+
|
83
|
+
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
|
84
|
+
|
85
|
+
super.onActivityResult(requestCode, resultCode, resultData);
|
86
|
+
|
87
|
+
if (requestCode == RESULT_PICK_IMAGEFILE && resultCode == Activity.RESULT_OK && resultData.getData() != null) {
|
88
|
+
|
89
|
+
Uri uri = resultData.getData();
|
90
|
+
|
91
|
+
final int takeFlags = resultData.getFlags()
|
92
|
+
|
93
|
+
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
|
94
|
+
|
95
|
+
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
|
96
|
+
|
97
|
+
try {
|
98
|
+
|
99
|
+
getContentResolver().takePersistableUriPermission(uri, takeFlags);
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
catch (SecurityException e){
|
104
|
+
|
105
|
+
e.printStackTrace();
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
url = uri.toString();
|
110
|
+
|
111
|
+
loadImage(uri);
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
private void loadImage(Uri uri) {
|
120
|
+
|
121
|
+
ParcelFileDescriptor pfDescriptor = null;
|
122
|
+
|
123
|
+
try {
|
124
|
+
|
125
|
+
pfDescriptor = getContentResolver().openFileDescriptor(uri, "r");
|
126
|
+
|
127
|
+
if (pfDescriptor != null) {
|
128
|
+
|
129
|
+
FileDescriptor fileDescriptor = pfDescriptor.getFileDescriptor();
|
130
|
+
|
131
|
+
Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor);
|
132
|
+
|
133
|
+
pfDescriptor.close();
|
134
|
+
|
135
|
+
imageView.setImageBitmap(bmp);
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
} catch (IOException e) {
|
140
|
+
|
141
|
+
e.printStackTrace();
|
142
|
+
|
143
|
+
} finally {
|
144
|
+
|
145
|
+
try {
|
146
|
+
|
147
|
+
if (pfDescriptor != null) {
|
148
|
+
|
149
|
+
pfDescriptor.close();
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
} catch (Exception e) {
|
154
|
+
|
155
|
+
e.printStackTrace();
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
```
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
```xml
|
170
|
+
|
171
|
+
<?xml version="1.0" encoding="utf-8"?>
|
172
|
+
|
173
|
+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
174
|
+
|
175
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
176
|
+
|
177
|
+
xmlns:tools="http://schemas.android.com/tools"
|
178
|
+
|
179
|
+
android:layout_width="match_parent"
|
180
|
+
|
181
|
+
android:layout_height="match_parent"
|
182
|
+
|
183
|
+
tools:context=".MainActivity">
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
<ImageView
|
188
|
+
|
189
|
+
android:id="@+id/imageEdit"
|
190
|
+
|
191
|
+
android:layout_width="match_parent"
|
192
|
+
|
193
|
+
android:layout_height="300dp"
|
194
|
+
|
195
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
196
|
+
|
197
|
+
app:layout_constraintEnd_toEndOf="parent"
|
198
|
+
|
199
|
+
app:layout_constraintHorizontal_bias="0.498"
|
200
|
+
|
201
|
+
app:layout_constraintStart_toStartOf="parent"
|
202
|
+
|
203
|
+
app:layout_constraintTop_toTopOf="parent"
|
204
|
+
|
205
|
+
app:layout_constraintVertical_bias="0.476" />
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
<Button
|
210
|
+
|
211
|
+
android:id="@+id/button"
|
212
|
+
|
213
|
+
android:layout_width="wrap_content"
|
214
|
+
|
215
|
+
android:layout_height="wrap_content"
|
216
|
+
|
217
|
+
android:text="load"
|
218
|
+
|
219
|
+
app:layout_constraintBottom_toBottomOf="@id/save"
|
220
|
+
|
221
|
+
app:layout_constraintEnd_toEndOf="parent"
|
222
|
+
|
223
|
+
app:layout_constraintStart_toStartOf="parent"
|
224
|
+
|
225
|
+
app:layout_constraintTop_toBottomOf="@+id/imageEdit" />
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
<Button
|
230
|
+
|
231
|
+
android:id="@+id/save"
|
232
|
+
|
233
|
+
android:layout_width="wrap_content"
|
234
|
+
|
235
|
+
android:layout_height="wrap_content"
|
236
|
+
|
237
|
+
android:text="save"
|
238
|
+
|
239
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
240
|
+
|
241
|
+
app:layout_constraintEnd_toEndOf="parent"
|
242
|
+
|
243
|
+
app:layout_constraintStart_toStartOf="parent"
|
244
|
+
|
245
|
+
app:layout_constraintTop_toBottomOf="@+id/button" />
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
250
|
+
|
251
|
+
```
|