質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

1909閲覧

ギャラリーから読み込んだ画像を、アプリの画面に保存させたい

android_app_tw

総合スコア4

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2020/08/28 05:13

編集2020/09/07 13:13

現在、アプリ開発の勉強中です。
ギャラリーから画像を持ってきて、アプリの画面に表示させるところまでできましたが、その先の画面内に保存し、アプリを閉じて再起動させたときに残っているようにするやり方がわかりません。
今できているコードを以下に載せます。
MainActivity

public class MainActivity extends AppCompatActivity { private static final int RESULT_PICK_IMAGEFILE = 1001; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.imageView1); Button inputbutton = findViewById(R.id.button); inputbutton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); startActivityForResult(intent, RESULT_PICK_IMAGEFILE); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent resultData) { super.onActivityResult(requestCode, resultCode, resultData); if (requestCode == RESULT_PICK_IMAGEFILE && resultCode == Activity.RESULT_OK) { if (resultData.getData() != null) { ParcelFileDescriptor pfDescriptor = null; try { Uri uri = resultData.getData(); pfDescriptor = getContentResolver().openFileDescriptor(uri, "r"); if (pfDescriptor != null) { FileDescriptor fileDescriptor = pfDescriptor.getFileDescriptor(); Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor); pfDescriptor.close(); imageView.setImageBitmap(bmp); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (pfDescriptor != null) { pfDescriptor.close(); } } catch (Exception e) { e.printStackTrace(); } } } } } //このBitmap画像をどのように画面に保存するのかが問題// //アプリを閉じて再起動させたときに、表示させた画像が残っているようにしたい// }
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity"> <ImageView android:id="@+id/imageEdit" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.476" app:srcCompat="@drawable/ic_launcher_foreground" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageEdit" /> //ここに保存ボタンを追加したい// </androidx.constraintlayout.widget.ConstraintLayout>

ここにもう一つ,保存ボタンを追加(SaveOnClickなど使って)したいと考えています。
どのような処理を追加すればよいのかわからない状態なので、教えていただければと思います。
また、可能であれば追加するプログラムを全文載せていただければと思います。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

saveボタンを押した時にonActivityResultで取得したUriをtoStringで文字列化して、SharedPrefrenceかSQLite等に保存してはどうでしょうか?

画面再起動時は保存した文字列を読み込んでUriに戻しBitmapを表示してはどうですか?

java

1public class MainActivity extends AppCompatActivity { 2 private static final int RESULT_PICK_IMAGEFILE = 1001; 3 private static final String KEY = "key"; 4 private ImageView imageView; 5 private String url = null; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 imageView = findViewById(R.id.imageEdit); 12 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); 13 String lastUrl = sp.getString(KEY, null); 14 if(lastUrl != null) { 15 loadImage(Uri.parse(lastUrl)); 16 } 17 findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { 18 public void onClick(View v) { 19 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); 20 intent.addCategory(Intent.CATEGORY_OPENABLE); 21 intent.setType("*/*"); 22 startActivityForResult(intent, RESULT_PICK_IMAGEFILE); 23 } 24 }); 25 26 findViewById(R.id.save).setOnClickListener(new View.OnClickListener() { 27 public void onClick(View v) { 28 if(url != null) { 29 sp.edit().putString(KEY,url).apply(); 30 } 31 } 32 }); 33 } 34 35 @RequiresApi(api = Build.VERSION_CODES.KITKAT) 36 @Override 37 public void onActivityResult(int requestCode, int resultCode, Intent resultData) { 38 super.onActivityResult(requestCode, resultCode, resultData); 39 if (requestCode == RESULT_PICK_IMAGEFILE && resultCode == Activity.RESULT_OK && resultData.getData() != null) { 40 Uri uri = resultData.getData(); 41 final int takeFlags = resultData.getFlags() 42 & (Intent.FLAG_GRANT_READ_URI_PERMISSION 43 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 44 try { 45 getContentResolver().takePersistableUriPermission(uri, takeFlags); 46 } 47 catch (SecurityException e){ 48 e.printStackTrace(); 49 } 50 url = uri.toString(); 51 loadImage(uri); 52 } 53 } 54 55 private void loadImage(Uri uri) { 56 ParcelFileDescriptor pfDescriptor = null; 57 try { 58 pfDescriptor = getContentResolver().openFileDescriptor(uri, "r"); 59 if (pfDescriptor != null) { 60 FileDescriptor fileDescriptor = pfDescriptor.getFileDescriptor(); 61 Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor); 62 pfDescriptor.close(); 63 imageView.setImageBitmap(bmp); 64 } 65 } catch (IOException e) { 66 e.printStackTrace(); 67 } finally { 68 try { 69 if (pfDescriptor != null) { 70 pfDescriptor.close(); 71 } 72 } catch (Exception e) { 73 e.printStackTrace(); 74 } 75 } 76 } 77}

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <ImageView 10 android:id="@+id/imageEdit" 11 android:layout_width="match_parent" 12 android:layout_height="300dp" 13 app:layout_constraintBottom_toBottomOf="parent" 14 app:layout_constraintEnd_toEndOf="parent" 15 app:layout_constraintHorizontal_bias="0.498" 16 app:layout_constraintStart_toStartOf="parent" 17 app:layout_constraintTop_toTopOf="parent" 18 app:layout_constraintVertical_bias="0.476" /> 19 20 <Button 21 android:id="@+id/button" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="load" 25 app:layout_constraintBottom_toBottomOf="@id/save" 26 app:layout_constraintEnd_toEndOf="parent" 27 app:layout_constraintStart_toStartOf="parent" 28 app:layout_constraintTop_toBottomOf="@+id/imageEdit" /> 29 30 <Button 31 android:id="@+id/save" 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:text="save" 35 app:layout_constraintBottom_toBottomOf="parent" 36 app:layout_constraintEnd_toEndOf="parent" 37 app:layout_constraintStart_toStartOf="parent" 38 app:layout_constraintTop_toBottomOf="@+id/button" /> 39 40</androidx.constraintlayout.widget.ConstraintLayout>

投稿2020/09/11 03:18

編集2020/09/14 14:46
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

android_app_tw

2020/09/14 10:33

morisakisanさん、回答ありがとうございます。色々試行錯誤して試してみたのですが、私自身初心者なので、どこにどうプログラムを書いていいのかわからない状態です。 もしよろしければもう少し詳しく教えて頂くことは可能でしょうか。
退会済みユーザー

退会済みユーザー

2020/09/14 14:46

暇つぶしにSharedPreferencesでやってみました。
android_app_tw

2020/09/15 01:45

ありがとうございます!! 助かります!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問