2017-05-18 31 views
0

我捕獲圖像並將其存儲在移動設備的存儲中,但是當我得到此圖像時,它無法在圖像視圖中顯示任何圖像。我已經嘗試了很多代碼來從文件中獲取圖像,但它們都不在我的模擬器或真正的三星設備中工作。從存儲器中獲取不在Android中的圖像

enter code here 

imageHolder = (ImageView)findViewById(R.id.captured_photo); 
    Button capturedImageButton = (Button)findViewById(R.id.photo_button); 
    capturedImageButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(photoCaptureIntent, requestCode); 
     } 
    }); 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if(this.requestCode == requestCode && resultCode == RESULT_OK){ 
     Bitmap bitmap = (Bitmap)data.getExtras().get("data"); 

     String partFilename = currentDateFormat(); 
     storeCameraPhotoInSDCard(bitmap, partFilename); 


     // display the image from SD Card to ImageView Control 
     String storeFilename = "photo_" + partFilename + ".jpg"; 
     Bitmap mBitmap = getImageFileFromSDCard(storeFilename); 
     imageHolder.setImageBitmap(mBitmap); 

    } 
} 

    private String currentDateFormat(){ 
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HH_mm_ss"); 
    String currentTimeStamp = dateFormat.format(new Date()); 
    return currentTimeStamp; 
} 

private void storeCameraPhotoInSDCard(Bitmap bitmap, String currentDate){ 
    File outputFile = new File(Environment.getExternalStorageDirectory(), "photo_" + currentDate + ".jpg"); 
    try { 
     FileOutputStream fileOutputStream = new FileOutputStream(outputFile); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); 
     fileOutputStream.flush(); 
     fileOutputStream.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
private Bitmap getImageFileFromSDCard(String filename){ 
    /* Bitmap bitmap = null; 
    File imageFile = new File(Environment.getExternalStorageDirectory() + filename); 
    try { 
     FileInputStream fis = new FileInputStream(imageFile); 
     bitmap = BitmapFactory.decodeStream(fis); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    return bitmap; */ 

    File imageFile = new File(Environment.getExternalStorageDirectory() + filename); 
    // File imgFile = new File(filename); 
      //("/sdcard/Images/test_image.jpg"); 
    Bitmap myBitmap; 

    if(imageFile.exists()){ 

     myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath()); 

     // ImageView myImage = (ImageView) findViewById(R.id.imageviewTest); 

     // myImage.setImageBitmap(myBitmap); 

     return myBitmap; 

    } 
    return null; 
} 
+0

http://stackoverflow.com/questions/30860646/upload-image-to-server-from-gallary-or-camera-android – jagapathi

回答

0

的所有首先確保您已聲明「訪問外部存儲」和「訪問硬件的攝像機」的「AndroidManifest」權限,如果你使用的是Android版本23或23+,那麼你必須在採取權限運行。 如果這不是問題,那麼使用下面給出的代碼,它對我來說工作正常。

對於攝像頭:

Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(takePicture, ACTION_REQUEST_CAMERA); 

對於畫廊:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
      intent.setType("image/*"); 

      Intent chooser = Intent.createChooser(intent, "Choose a Picture"); 
      startActivityForResult(chooser, ACTION_REQUEST_GALLERY); 

OnActivityResultMethod:

/** 
* helper to retrieve the path of an image URI 
*/ 
public String getPath(Uri uri) { 
    // just some safety built in 
    if(uri == null) { 
     // TODO perform some logging or show user feedback 
     return null; 
    } 
    // try to retrieve the image from the media store first 
    // this will only work for images selected from gallery 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null); 
    if(cursor != null){ 
     int column_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } 
    // this is our fallback here 
    return uri.getPath(); 
} 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 

     switch (requestCode) { 
      case ACTION_REQUEST_GALLERY: 
       Uri galleryImageUri = data.getData(); 
       try{ 
        Log.e("Image Path Gallery" , getPath(getActivity() , galleryImageUri)); 
        selectedImagePath = getPath(getActivity() , galleryImageUri); 
       } catch (Exception ex){ 
        ex.printStackTrace(); 
        Log.e("Image Path Gallery" , galleryImageUri.getPath()); 
        selectedImagePath = galleryImageUri.getPath(); 
       } 

       break; 

      case ACTION_REQUEST_CAMERA: 
       // Uri cameraImageUri = initialURI; 
       Uri cameraImageUri = data.getData(); 
       Log.e("Image Path Camera" , getPath(cameraImageUri)); 
       selectedImagePath = getPath(cameraImageUri); 
       break; 
     } 

    } 
} 

方法來獲得圖像的路徑從相機返回

相關問題