0

我正在關注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(); 


    } 
+0

會發生什麼事,當你運行你的應用程序?你有錯誤嗎? –

+0

沒有錯誤。我運行主要活動,然後調用MediaStore.ACTION_IMAGE_CAPTURE。我拍了一張照片,最後回到了主要活動。 – Galabyca

回答

2

您當前正在將文件保存爲臨時文件,因此在應用程序生命週期後它不會保留在磁盤上。使用類似:

ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes); 
File f = new File(Environment.getExternalStorageDirectory() + [filename]) 

,然後創建一個FileOutputStream寫信給它。

FileOutStream fo = new FileOutputStream(f); 
fo.write(bytes.toByteArray()); 
+0

我試過,但沒有任何變化:( – Galabyca

+0

我按照你的建議添加了一些代碼,我編輯了我的主帖。 – Galabyca

0

解決我的問題,我不得不將文件寫入到應用程序的數據文件夾和用戶MediaScannerConnection。我已經放置了一個用於測試的.txt文件,但是在它工作之後,您可以放入任何其他文件。

我將分享那些誰也有類似的問題的解決方案:

try 
    { 
     // Creates a trace file in the primary external storage space of the 
     // current application. 
     // If the file does not exists, it is created. 
     File traceFile = new File(((Context)this).getExternalFilesDir(null), "TraceFile.txt"); 
     if (!traceFile.exists()) 
      traceFile.createNewFile(); 
     // Adds a line to the trace file 
     BufferedWriter writer = new BufferedWriter(new FileWriter(traceFile, true /*append*/)); 
     writer.write("This is a test trace file."); 
     writer.close(); 
     // Refresh the data so it can seen when the device is plugged in a 
     // computer. You may have to unplug and replug the device to see the 
     // latest changes. This is not necessary if the user should not modify 
     // the files. 
     MediaScannerConnection.scanFile((Context)(this), 
       new String[] { traceFile.toString() }, 
       null, 
       null); 

    } 
    catch (IOException e) 
    { 
     Log.d("FileTest", "Unable to write to the TraceFile.txt file."); 
    }