Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm newbie in android. My question is how to set shared preferences in image view. I want to shared the image to another activity. Please help me because I'm stocked on it.. Please help me the explain me clearly and codes. Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
303 views
Welcome To Ask or Share your Answers For Others

1 Answer

The "standard" way to share data across Activities is usign the putExtraXXX methods on the intent class. You can put the image path in your intent:

Intent intent = new Intent(this,MyClassA.class);
intent.putExtra(MyClassA.IMAGE_EXTRA, imagePath);
startActivity(intent);

And you retrieve it and open it in your next Activity:

String filePath = getIntent().getStringExtra(MyClassA.IMAGE_EXTRA);

Here is an implementation of a function that opens and decodes the image and return a Bitmap object, notice that this function requires the image to be located in the assets folder:

private Bitmap getImageFromAssets(String assetsPath,int reqWidth, int reqHeight) {
    AssetManager assetManager = getAssets();

    InputStream istr;
    Bitmap bitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    try {
        istr = assetManager.open(assetsPath);
        bitmap = BitmapFactory.decodeStream(istr, null, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeStream(istr, null, options);
    } catch (IOException e) {
        return null;
    }

    return bitmap;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...