質問編集履歴

2

html表記変更

2023/02/09 01:11

投稿

rstaff
rstaff

スコア2

test CHANGED
File without changes
test CHANGED
@@ -25,7 +25,7 @@
25
25
  ここの引数の気もするのですが分かりませんでした。
26
26
 
27
27
  このソースもしくは違うやり方でも良いので絶対パスの取得方法をご教授下さい。
28
- ###追加質問
28
+ ### 追加質問
29
29
  実は言うとギャラリーで選択した画像をimageViewで表示させるのは出来たのですが、そのuriをプレファレンスデータに保存して再度開いた時に保存したプレファレンスデータから画像を表示させようとしています。
30
30
  しかし保存したデータから表示することが出来ませんでした。
31
31
 

1

追加質問を追記

2023/02/09 01:10

投稿

rstaff
rstaff

スコア2

test CHANGED
File without changes
test CHANGED
@@ -25,4 +25,60 @@
25
25
  ここの引数の気もするのですが分かりませんでした。
26
26
 
27
27
  このソースもしくは違うやり方でも良いので絶対パスの取得方法をご教授下さい。
28
+ ###追加質問
29
+ 実は言うとギャラリーで選択した画像をimageViewで表示させるのは出来たのですが、そのuriをプレファレンスデータに保存して再度開いた時に保存したプレファレンスデータから画像を表示させようとしています。
30
+ しかし保存したデータから表示することが出来ませんでした。
28
31
 
32
+ ▼ギャラリーを開いて、選択された写真のuriを取得し、imageviewに保存&プレファレンスデータに保存する処理です。
33
+ ```
34
+ private final ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
35
+ new ActivityResultContracts.StartActivityForResult(),
36
+ result -> {
37
+ if (result.getResultCode() == Activity.RESULT_OK) {
38
+ Intent resultData = result.getData();
39
+ if (resultData != null) {
40
+ try{
41
+ Uri uri = resultData.getData();
42
+
43
+ ParcelFileDescriptor pfDescriptor = getContentResolver().openFileDescriptor(uri, "r");
44
+ if(pfDescriptor != null){
45
+ FileDescriptor fileDescriptor = pfDescriptor.getFileDescriptor();
46
+ Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor);
47
+ pfDescriptor.close();
48
+ ImageView imageView = (ImageView) findViewById(R.id.page61Image);
49
+ imageView.setImageBitmap(bmp);
50
+
51
+ //プレファレンスデータに文字列データを登録
52
+ SharedPreferences.Editor editor = PreferencesData.edit();
53
+ editor.putString("Data61Photo", uri.toString());
54
+ editor.apply();
55
+
56
+ TextView textView62 = findViewById(R.id.page62Edit);
57
+ textView62.setText(uri.toString()); //P6-2のデータをテキストに表示
58
+ }
59
+
60
+
61
+ } catch (FileNotFoundException e) {
62
+ e.printStackTrace();
63
+ } catch (IOException e) {
64
+ e.printStackTrace();
65
+ }
66
+
67
+ }
68
+ }
69
+ });
70
+ ```
71
+ ▼保存したプレファレンスデータから再度imageviewに表示させる処理(このIntentに遷移すると落ちてしまいます)
72
+ ```
73
+ Data61Photo = PreferencesData.getString("Data61Photo", null); //データを取得
74
+ if (Data61Photo != null) {
75
+ Uri uri = Uri.parse(Data61Photo);
76
+
77
+ ParcelFileDescriptor pfDescriptor = getContentResolver().openFileDescriptor(uri, "r");
78
+ FileDescriptor fileDescriptor = pfDescriptor.getFileDescriptor();
79
+ Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor);
80
+ pfDescriptor.close();
81
+ ImageView imageView = (ImageView) findViewById(R.id.page61Image);
82
+ imageView.setImageBitmap(bmp);
83
+ }
84
+ ```