Android內存中位圖/圖像的保存與讀取我想要做的是,把圖像保存到電話的內存中。(不是SD卡).我該怎么做?我已經把圖像直接從相機到我的應用程序中的圖像視圖,這一切都工作得很好?,F在我想要的是將這張圖像從圖像視圖保存到我的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
提交
取消
