1

i'm trying to do an activity which can show picture from gallery. But my Activity needs to remember bitmap uri for another time so it can always open that picture. here is my .java

edited

i added something else to convert uri to string then string to uri but it doesn't work. Can someone help?

public class DersProgram extends Activity {  private static int RESULT_LOAD_IMAGE = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dersprogrami);

    Button buttonLoadImage = (Button) findViewById(R.id.button1);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, 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 uri = data.getData();         
        String BitmapURI;
        BitmapURI = uri.toString();
        uri = Uri.parse(BitmapURI);


        SharedPreferences sharedPreferences = getSharedPreferences("BitmapURI", Context.MODE_PRIVATE);
        sharedPreferences.edit().putString("BitmapURI", "your URI as a String").apply();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(uri,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.resim);

        Bitmap bmp = null;
        try {
            bmp = getBitmapFromUri(uri);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        imageView.setImageBitmap(bmp);

    }


}



private Bitmap getBitmapFromUri(Uri uri) throws IOException {
    ParcelFileDescriptor parcelFileDescriptor =
            getContentResolver().openFileDescriptor(uri, "r");
    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
    Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
    parcelFileDescriptor.close();
    return image;
}

}

I want to open picture which is called before. Please help me(also i am a beginner) sorry for my English.

S.Doan
  • 11
  • 5

1 Answers1

0

As said, use SharedPreferences.

store your bitmapURI as a String in your Activity:

SharedPreferences sharedPreferences = getSharedPreferences("some string", Context.MODE_PRIVATE);
sharedPreferences.edit().putString("BitmapURI", "your URI as a String").apply();

and retrieve it whenever your want:

String bitmapURI = sharedPreferences.getString("BitmapURI", "default string if BitmapURI is not set");
Pierre R-A
  • 509
  • 9
  • 13
  • By the way, for Uri to String transformation look there: http://stackoverflow.com/questions/16324482/convert-uri-to-string-and-string-to-uri. – Pierre R-A Dec 26 '15 at 09:13