我正在關注Android應用程序的Google Camera Tutorial。此時,我可以拍攝照片,保存照片,顯示路徑並將位圖顯示到ImageView中。如何保存圖片並在PC上訪問?
這裏是logcat的一個爲例,當我問我只是拍了照片的絕對路徑:現在
D/PATH:: /storage/emulated/0/Pictures/JPEG_20160210_140144_217642556.jpg
,我想通過USB將其傳送的PC上。當我打開設備存儲器時,我可以看到公用文件夾Picture
,這是我之前在代碼中用變量Environment.DIRECTORY_PICTURES
調用的。但是,此文件夾中沒有任何內容。
Screenshot of my device's folders
我不能在我的設備來測試插入SD卡。另外,我不想將圖片放入緩存目錄以防止刪除。
這是我在清單的權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
當在相機上的按鈕,用戶點擊:
dispatchTakePictureIntent();
[...]
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
這方法創建文件
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
Log.d("PATH:", image.getAbsolutePath());
return image;
}
我想我誤解了有關External Storage
的內容。 有人可以解釋我爲什麼不能保存圖片並在PC上訪問它嗎?謝謝!
- 編輯 -
閱讀下面的答案後,我就先在OnActivityResult
文件,並把它與Java IO保存。不幸的是,當我用Explorer查看時,圖片文件夾中沒有文件。
if (requestCode == REQUEST_TAKE_PHOTO) {
Log.d("AFTER", absolutePath);
// Bitmap bitmap = BitmapFactory.decodeFile(absolutePath);
// imageTest.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 2100, 3100, false));
moveFile(absolutePath, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString());
}
private void moveFile(String inputFile, String outputPath) {
InputStream in = null;
OutputStream out = null;
try {
//create output directory if it doesn't exist
File dir = new File (outputPath);
if (!dir.exists())
{
dir.mkdirs();
}
in = new FileInputStream(inputFile);
out = new FileOutputStream(outputPath + imageFileName + ".jpg");
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
// write the output file
out.flush();
out.close();
out = null;
// delete the original file
new File(inputFile).delete();
}
會發生什麼事,當你運行你的應用程序?你有錯誤嗎? –
沒有錯誤。我運行主要活動,然後調用MediaStore.ACTION_IMAGE_CAPTURE。我拍了一張照片,最後回到了主要活動。 – Galabyca