0

以下の私のコードは、ギャラリーから ImageButton に画像を配置しますが、常にアプリケーションを終了するか、別のアクティビティに移動すると、画像が保存されず、最初の背景が再び表示されます。

助けが必要です。ImageButton の背景として定義した画像を保存するにはどうすればよいですか

sharedpreferences について読みましたが、アプリでの使用方法がわかりません

-

- 私のクラス

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //Adding the picture bit   

    imgButton = (ImageButton) findViewById(R.id.AddPic);
    imgButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent GaleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(GaleryIntent, RESULT_LOAD_IMAGE);
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri SelectedImage = data.getData();
        String[] FilePathColumn = {MediaStore.Images.Media.DATA };

        Cursor SelectedCursor = getContentResolver().query(SelectedImage, FilePathColumn, null, null, null);
        SelectedCursor.moveToFirst();

        int columnIndex = SelectedCursor.getColumnIndex(FilePathColumn[0]);
        String picturePath = SelectedCursor.getString(columnIndex);
        SelectedCursor.close();

      //  Drawable d = new BitmapDrawable(getResources(),BitmapFactory.decodeFile(picturePath)); 
       // btnOpenGalery .setImageBitmap(d);
        imgButton.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        Toast.makeText(getApplicationContext(), picturePath, Toast.LENGTH_SHORT).show();

    }

}   

私のXML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

<ImageButton
    android:id="@+id/AddPic"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="center"
    android:gravity="left"
    android:onClick="AddPic"
    android:background="@drawable/ic_launcher" />

</LinearLayout>
4

2 に答える 2

0

以前はこのタスクを実行しませんでしたが、画像を Base64 文字列として Preferences に保存できると思います。その画像を再度取得したい場合は、Base64 文字列を対応する画像に変換します。このリンクに従って 画像を Base64 文字列に変換し、Base64 文字列を画像に変換するには、このリンクを参照してください。

于 2015-10-15T05:57:45.747 に答える