2013-12-10 139 views
1
public void onPictureTaken(byte[] data, Camera camera) 
     { 

      Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Folder/Raw Image"); 
      imagesFolder.mkdirs(); 
      Date d = new Date(); 
      CharSequence s = DateFormat.format("MM-dd-yy hh-mm-ss", d.getTime()); 
      File output = new File(imagesFolder, s.toString() + ".jpg"); 

      Uri uriSavedImage = Uri.fromFile(output); 
      imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 
      OutputStream imageFileOS; 

      try { 
       imageFileOS = getContentResolver().openOutputStream(uriSavedImage); 
       imageFileOS.write(data); 
       imageFileOS.flush(); 
       imageFileOS.close(); 

       Toast.makeText(AndroidCamera.this, 
         "Image saved: ", 
         Toast.LENGTH_LONG).show(); 

       } 
      catch (FileNotFoundException e) 
       { 
       e.printStackTrace(); 
       } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      finally 
      {} 
      Log.d("Log", "onPictureTaken - jpeg"); 
File filenew = new File(android.os.Environment.getExternalStorageDirectory(),"Folder/Raw Image"); 

      bitmap=BitmapFactory.decodeFile(filenew.getAbsolutePath()); 

每當我捕捉圖像時,圖像保存到文件夾「文件夾」中的子文件夾「原始圖像」。我需要檢索這個圖像進行進一步處理。所以我使用Bitmapfactory.options。所有這些都需要點擊一下按鈕。每個保存的圖像都必須進行處理。我如何以這種方式檢索。從文件夾中檢索圖像:Android

回答

0

如果你正在拍照,你可以做的是startActivityForResult,在這個過程中你必須使用URIURI是您爲圖片指定的路徑。

現在你onActivityResult(..)通話過程中,你可以做到以下幾點:

try { 
    Bitmap thumb = MediaStore.Images.Media.getBitmap(
      activity.getContentResolver(), fileUri); 
    photo.setImageBitmap(thumb); 
} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

哪裏fileUri是我說的是圖像的URI。這裏photoImageView。另外,在使用不同設備時,可能會出現錯誤,因此我建議您將URI路徑保存在SharedPreference中。

0

您都指向了錯誤的道路:

Environment.getExternalStorageDirectory().toString() + "/"+ Folder + "Raw Image"+ ".jpg"; 

但你已經創建的文件夾這樣,

嘗試這種方式,它可以幫助你。

File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Folder/RawImage"); 
     imagesFolder.mkdirs(); 

     File output = new File(imagesFolder,"temp" + ".jpg"); 

上面我們已經聲明瞭一個靜態文件名。該文件將被替換爲每個圖像捕獲。 現在你知道文件路徑,

String mFilePath = Environment.getExternalStorageDirectory() + "Folder/RawImage/temp.jpg"; 

您可以使用此文件路徑以下列方式來顯示位圖。

myImage.setImageUri(Uri.fromFile(new File("Your file path"))); 
Or with: 
myImage.setImageUri(Uri.parse(new File("Your file path").toString())); 

你可以得到位圖:

Bitmap bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(Your image URI)); 
+0

但我不想來取代它。每次我想檢索捕獲的圖像到一個位圖具有相同的名稱,然後我想要處理它,並希望保存處理後的圖像具有相同的名稱,但到一個新的文件夾..我希望你有我的問題 – Aswathy

+0

什麼,你應該提及正確的道路。沒有這樣的文件異常會拋出你的代碼。在使用該文件之前,請檢查文件是否存在。 myFile.isExist();如果您使用日期,很難找到文件名。如果你提到文件名,你可以適當地使用。並將圖像保存到新文件夾中。 –

相關問題