Android内存中位图/图像的保存与读取我想要做的是,把图像保存到电话的内存中。(不是SD卡).我该怎么做?我已经把图像直接从相机到我的应用程序中的图像视图,这一切都工作得很好。现在我想要的是将这张图像从图像视图保存到我的Android设备的内部内存中,并在需要时访问它。有人能指点我怎么做吗?我是一个有点新的Android,所以请,我会感谢如果我可以有一个详细的程序。
3 回答
泛舟湖上清波郎朗
TA贡献1818条经验 获得超3个赞
private String saveToInternalStorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,"profile.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return directory.getAbsolutePath();
}说明:
private void loadImageFromStorage(String path){
try {
File f=new File(path, "profile.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
ImageView img=(ImageView)findViewById(R.id.imgPicker);
img.setImageBitmap(b);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}}
皈依舞
TA贡献1851条经验 获得超3个赞
public void saveImage(Context context, Bitmap bitmap, String name, String extension){
name = name + "." + extension;
FileOutputStream fileOutputStream;
try {
fileOutputStream = context.openFileOutput(name, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}}public Bitmap loadImageBitmap(Context context,String name,String extension){
name = name + "." + extension FileInputStream fileInputStream Bitmap bitmap = null;
try{
fileInputStream = context.openFileInput(name);
bitmap = BitmapFactory.decodeStream(fileInputStream);
fileInputStream.close();
} catch(Exception e) {
e.printStackTrace();
}
return bitmap;}添加回答
举报
0/150
提交
取消
