[英]can't save file in android file system
我正在嘗試用相機捕獲照片並將其保存(稍后進行預覽),它似乎可以與模擬器一起使用,但是當我在GalaxyS上使用它時-它不會保存文件(我使用RootExplorer
進行檢查),並且沒有預覽。
我究竟做錯了什么?
保存文件的代碼:
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outStream = null;
try {
// Write to SD Card
String filename = "captured_image.jpg";
Log.d("@@--File name--@@", filename);
outStream = openFileOutput(filename, Context.MODE_WORLD_READABLE); // <9>
outStream.write(data);
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) { // <10>
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d(TAG, "onPictureTaken - jpeg");
}
顯示代碼:
ImageView imagePrev = (ImageView) findViewById(R.id.image_capturedimagepreview_preview);
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeStream(openFileInput("captured_image.jpg"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
imagePrev.setImageBitmap(bmp);
您將圖像存儲在哪里? 您是否嘗試過使用絕對路徑? 清單中是否具有讀/寫外部權限? 我在程序中使用了類似的方法將映像存儲在與程序包名稱相同的目錄中。
File path = new File(Environment.getExternalStorageDirectory(), context.getPackageName() );
File imagePath = new File(path,"capture_image.jpg");
編輯:如果您是第一次使用sd卡作為給定的包名稱,則需要在嘗試寫入目錄之前創建目錄。
我認為我發現了問題。 而不是outStream = openFileOutput(filename, Context.MODE_WORLD_READABLE);
我應該使用outStream = getApplicationContext().openFileOutput(filename, Context.MODE_WORLD_READABLE);
但是現在我面臨着一個新文件-該文件似乎已損壞,原因是當我使用Android的查看器打開該文件時,它只是黑色的,其大小始終為18474字節。
有任何想法嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.