android - android中捕获的图像不是从另一个 Activity 中获取的

标签 android android-intent camera

我有以下情况:从我的第一个 Activity 开始,我从相机拍摄图像,然后打开一个新 Activity ,其中应该预览 iage 并将其作为 httpPOST 请求中的 FILE 参数上传到服务器。

在我的第一个 Activity 中,我使用了这段代码:

public void onClick(View v) {
   String fileName = "temp.jpg";                    
   ContentValues values = new ContentValues();
   values.put(MediaStore.Images.Media.TITLE,fileName);
   mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
   Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
   startActivityForResult(cameraIntent,RESULT_LOAD_IMAGE_FROM_CAMERA);
}

onMyActivityResult我有:

if (requestCode == RESULT_LOAD_IMAGE_FROM_CAMERA
                && resultCode == RESULT_OK && null != data) {
            System.out.println("Photo selected from the camera");

            String[] projection = {MediaStore.Images.Media.DATA};
            //Uri selectedImage = data.getData();

            Cursor cursor = getContentResolver().query(mCapturedImageURI, projection, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

            //int columnIndex = cursor.getColumnIndex(projection[0]);
           //file path of captured image
            picturePath = cursor.getString(columnIndex); 
            //file path of captured image
            File f = new File(picturePath);
            String filename = f.getName();

            Toast.makeText(CommunityMain.this, "Your Path:"+picturePath, 2000).show();
            Toast.makeText(CommunityMain.this, "Your Filename:"+filename, 2000).show();
            cursor.close();
       Intent myIntent = new Intent(CommunityMain.this,PhotoPostActivity.class);
       startActivity(myIntent);

图像保存在图库中,我确实看到了 picturePath。

当我的 photoPost Activity 打开时,我确实有:

f = new File(CommunityMain.picturePath);
        System.out.println("Picture path:"+CommunityMain.picturePath);
        ImageButton image = (ImageButton) findViewById(R.id.photo1);
        image.setImageBitmap(BitmapFactory
                .decodeFile(CommunityMain.picturePath));

图片路径是一样的,但是我的ImageButton没有设置

我明白了:

12-08 17:37:22.670: E/BitmapFactory(13357): Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/1386524213331.jpg: open failed: ENOENT (No such file or directory)

但路径与 toast 消息相同。我错过了什么?

我还需要能够获取文件 f,因为我的 HttpPost 请求需要它。

编辑:问题出在 picturePath 上。我得到的图像与图像的保存方式不同? 我看到一个像上面的数字,照片中的实际路径有类似 IMG_2313.jpg 的东西,例如。

最佳答案

您应该使用不同的技术。尝试为相机提供您希望写入图像的文件网址,如下所示:https://stackoverflow.com/a/16392587/693752 .接下来,读取该文件而不是读取相机提供的内容。

关于android - android中捕获的图像不是从另一个 Activity 中获取的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20456744/

相关文章:

iphone - 检测 iPhone 上相机光圈何时打开

java - 菜单 Intent 上的相机预览卡住

java - 如何在以下代码中创建电子邮件和密码的运行时条目?

android 将 listview 项目高度设置为所有 child 中最高的,所以看起来像带边框的表格

android - 调用 Android Activity

android - Intent.setType() 返回Android中所有类型的文件

android - 如何在 Intent 之间传递 bool 值

ios - 如何更改某些功能以与 iOS 10 或更低版本兼容,以便我的 snapchat 中的某些功能(如相机 View Controller )

android - 不同的 Activity 或相同的 Activity 但布局不同

android - 与 GoogleServices 插件并行使用 Android DataBinding 插件