无法致力于onActivityresult

问题描述 投票:0回答:3
private static final int CAMERA_REQUEST = 1337;
private void showCamera() {
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra("category", "camera");
    startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

我用这段代码从相机中挑选一张图片。这是我的活动结果

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE_REQUEST ) {
        filePath = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);
            Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    else if (requestCode == CAMERA_REQUEST) {
        filePath = data.getData();
            Log.i("hello", "REQUEST cALL");
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), filePath);
                imageView.setImageBitmap(bitmap);

            } catch (Exception e) {
                Log.i("hello", "Exception" + e.getMessage());
            }
    }

相机很好。我可以抓住它

但为什么imageview无法从相机中挑选照片?

但如果我从存储中挑选,imageview可以改变图像。你能看到假代码吗?

java android
3个回答
0
投票

试试这个,希望它能帮到你。

 if (requestCode == CAMERA_REQUEST) {   
        Bitmap bitmap= (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(bitmap);  
  }  

0
投票
data.getData();

这个没有给你一个被捕获图像的filepath

你可以按照这个

Getting path of captured image in Android using camera intent

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  

        Bitmap photo = (Bitmap) data.getExtras().get("data"); 

       //get the URI of the bitmap from camera result
        Uri uri = getImageUri(getApplicationContext(), photo);


       // convert the URI to its real file path.
        String filePath = getRealPathFromURI(uri);

}

此方法获取可用于将其转换为文件路径的位图的URI

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

此方法将URI转换为文件路径

public String getRealPathFromURI(Uri uri) {
    String path = "";
    if (getContentResolver() != null) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            path = cursor.getString(idx);
            cursor.close();
        }
    }
    return path;
}

0
投票

要使用图像文件路径,您必须执行以下操作

1)编写一种方法来创建图像文件

String currentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
    imageFileName,  /* prefix */
    ".jpg",         /* suffix */
    storageDir      /* directory */
);

 // Save a file: path for use with ACTION_VIEW intents
  currentPhotoPath = image.getAbsolutePath();
  return image;
}

2)调用相机意图

private void showCamera() {
  Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

    File photoFile = null;
    try {
        photoFile = createImageFile();
    } catch (IOException ex) {
        // Error occurred while creating the File

    }
    // Continue only if the File was successfully created
    if (photoFile != null) {
        Uri photoURI = FileProvider.getUriForFile(this,
                                              "com.example.android.fileprovider",
                                              photoFile);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
    }
  }
}

3)现在,您需要配置FileProvider。在应用的清单中,向您的应用添加提供商:

<application>
 ...
 <provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths"></meta-data>
 </provider>
 ...
</application>

4)创建资源文件qazxsw poi

res/xml/file_paths.xml

注意:确保将<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" /> </paths> 替换为应用的实际包名称。

5)获取imageFilePath

com.example.package.name

希望它按预期工作。有关详细信息,您可以看到谷歌if (requestCode == CAMERA_REQUEST) { Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), currentPhotoPath); imageView.setImageBitmap(bitmap); // or you can use Glide to show image Glide.with(this).load(currentPhotoPath).into(imageView); }

© www.soinside.com 2019 - 2024. All rights reserved.