将图像复制到内存
Posted
技术标签:
【中文标题】将图像复制到内存【英文标题】:Copy image to internal memory 【发布时间】:2011-05-07 08:40:36 【问题描述】:如何并在之后恢复?
【问题讨论】:
恢复是什么意思?你能说得更具体点吗? 【参考方案1】:您可以使用以下 openFileOutput() 和 Bitmap.compress() 的组合将图像存储到内部存储。
// 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);
【讨论】:
【参考方案2】:我会按照以下方式进行:
//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;
【讨论】:
我需要在内存中复制一张图片以上是关于将图像复制到内存的主要内容,如果未能解决你的问题,请参考以下文章