How I copy a image to internal memory and recover after that?
Asked
Active
Viewed 7,683 times
2 Answers
12
You can use the following combination of openFileOutput() and Bitmap.compress() to store the image to the internal storage.
// Create a BitMap object of the image to be worked with
final Bitmap bm = BitmapFactory.decodeStream( ..some image.. );
// The openfileOutput() method creates a file on the phone/internal storage in the context of your application
final FileOutputStream fos = openFileOutput("my_new_image.jpg", Context.MODE_PRIVATE);
// Use the compress method on the BitMap object to write image to the OutputStream
bm.compress(CompressFormat.JPEG, 90, fos);

Elijah Cornell
- 431
- 2
- 5
1
I would do it in a following way:
//read bitmap from resource
public InputStream getBitmapStreamFromResource(Context context, int recourceId)
{
return context.getResources().getAssets().open(recourceId);
}
// convert stream into Bitmap
public Bitmap getBitmapFromStream(InputStream is)
{
return BitmapFactory.decodeStream(is);
}
//inflate bitmap in a given parent View
public View inflateToView(Context context, ViewGroup parent, Bitmap bitmap)
{
ViewGroup layout=new LinearLayout(context);
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ImageView imageView=new ImageView(context);
imageView.setEnabled(true);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(bitmap);
layout.addView(imageView);
parent.addView(layout);
return layout;
}

Barmaley
- 16,638
- 18
- 73
- 146