2017-02-10 85 views
1

基本上我想要做的是創建一個簡單的應用程序,它使用默認的攝像頭應用程序拍照,然後我想在ImageView中的新活動中顯示該圖像,但是我什麼都試過導致無法在ImageView中顯示攝像機活動的結果

java.io.FileNotFoundException: /storage/emulated/0/imageToProcess.jpg: open failed: ENOENT (No such file or directory)

我已經包含了正確的權限(甚至太多)在清單:

<uses-permission android:name="android.permission.CAMERA" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

,我也請求手動權限,如果需要在t中他的主要活動:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
    if(checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { 
      requestPermissions(new String[]{Manifest.permission.CAMERA}, 1); 
    } 
    if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 
     requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); 
    } 
    if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 
     requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1); 
    } 
} 

所以我不認爲這個問題是與讀/寫權限。

在我的主要活動的XML文件,我創建了一個按鈕,一個onClick監聽器調用下面的方法:

public void openCamera(View view) { 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    File f = new File(Environment.getExternalStorageDirectory() + File.separator + "imageToProcess.jpg"); 
    imgUri = Uri.fromFile(f); 
    intent.putExtra("imgUri", imgUri.toString()); 
    startActivityForResult(intent, TAKE_PICTURE); 
} 

這是我定義爲創建一個新的意圖和傳遞的onActivityResult()方法URI到新的活動:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    super.onActivityResult(requestCode, resultCode, intent); 
    if(requestCode == TAKE_PICTURE && resultCode == Activity.RESULT_OK) { 
     Intent camResult = new Intent(this, ShowCameraResult.class); 
     camResult.putExtra("imgUri", imgUri.toString()); 
     startActivity(camResult); 
    } 
} 

注:imgUri和TAKE_PICTURE在類的頂部定義爲protected static final int TAKE_PICTURE = 1;private Uri imgUri;

最後,這裏是我的ShowCameraResult活動

public class ShowCameraResult extends AppCompatActivity { 
    private Bitmap mImageBitmap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_show_camera_result); 
     ImageView imageView = (ImageView)findViewById(R.id.imageView); 
     String filePath = getIntent().getStringExtra("imgUri"); 
     Log.d("ShowCameraResult", "directory: " + filePath); 
     try { 
      mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(filePath)); 
      imageView.setImageBitmap(mImageBitmap); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

的Log.d方法調用上述返回

ShowCameraResult: directory: file:///storage/emulated/0/imageToProcess.jpg 

這使我相信,從攝像頭的圖像結果是不實際被保存到我的imageToProcess.jpg文件,但我可能是非常錯誤的,因爲這是我工作的第一個真正的Android項目。

所以我的問題是:我哪裏錯了?

任何幫助非常感謝,謝謝!

+0

測試設備上不是模擬器 –

+0

@Dhara我在銀河S5 – Austin

+0

它對其他三星測試中的錯誤檢測設備 –

回答

0

ITs返回一個文件,而不是內容解析器uri。用BitmapFactory解碼文件。

+0

它返回一個文件的URI。使用BitmapFactory.decodeFile獲取文件的位圖。然後顯示。不需要內容解析器,它不返回UR。它將文件存儲在您要求的地方。 –

+0

哦,並注意 - 你不需要相機的權限來啓動相機的意圖。只有當你計劃在你的應用程序中拍攝照片。 –

0
private void dispatchTakePictureIntent() { 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
      startActivityForResult(takePictureIntent, TAKE_PICTURE); 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     Log.d(TAG, "onActivityResult: "+data); 
     if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) { 
      Bundle extras = data.getExtras(); 
      Bitmap imageBitmap = (Bitmap) extras.get("data"); 
      Log.d(TAG, "onActivityResult: "+imageBitmap); 
      imageView.setImageBitmap(imageBitmap); 
     } 
    } 

SenderActivity:

Intent _intent = new Intent(this, newscreen.class); 
Bitmap _bitmap; // your bitmap 
ByteArrayOutputStream _bs = new ByteArrayOutputStream(); 
_bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs); 
i.putExtra("byteArray", _bs.toByteArray()); 
startActivity(i); 

ReciverActivity

if(getIntent().hasExtra("byteArray")) { 
ImageView _imv= new ImageView(this); 
Bitmap _bitmap = BitmapFactory.decodeByteArray(
     getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);   
_imv.setImageBitmap(_bitmap); 
}