2017-02-10 58 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); 
} 
相关问题