当前位置: 首页 > 知识库问答 >
问题:

无法打开捕获的或库中选择的图像

亢仰岳
2023-03-14
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
  //  return inflater.inflate(R.layout.fragment_images, container, false);
    View v = inflater.inflate(R.layout.fragment_images, container, false);
    FloatingActionButton btnCamera = (FloatingActionButton) v.findViewById(R.id.btnCamera);
    FloatingActionButton btnFolder = (FloatingActionButton) v.findViewById(R.id.btnFolder);

    btnCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            File imageFolder = new File(Environment.getExternalStorageDirectory(), "/My Children");
            imageFolder.mkdir();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
            String timestamp = simpleDateFormat.format(new Date());
            File image = new File(imageFolder, timestamp+ ".jpg");
            Uri uriImage = Uri.fromFile(image);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uriImage);
            startActivityForResult(intent, TAKE_PICTURE);
            }
    });
    btnFolder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(intent, SELECT_PICTURE);
        }
    });
    return v;

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
  //  return inflater.inflate(R.layout.fragment_images, container, false);
    View v = inflater.inflate(R.layout.fragment_images, container, false);
    FloatingActionButton btnCamera = (FloatingActionButton) v.findViewById(R.id.btnCamera);
    FloatingActionButton btnFolder = (FloatingActionButton) v.findViewById(R.id.btnFolder);

    btnCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            File imageFolder = new File(Environment.getExternalStorageDirectory(), "/My Children");
            imageFolder.mkdir();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
            String timestamp = simpleDateFormat.format(new Date());
            File image = new File(imageFolder, timestamp+ ".jpg");
            Uri uriImage = Uri.fromFile(image);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uriImage);
            startActivityForResult(intent, TAKE_PICTURE);
            }
    });
    btnFolder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(intent, SELECT_PICTURE);
        }
    });
    return v;
ImageView imageview = (ImageView) findViewById(R.id.imageview);
    Intent intent = getIntent();
    Bitmap bitmap = (Bitmap) intent.getParcelableExtra("image");
    imageview.setImageBitmap(bitmap);

logcat

 E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.smiles.mychildren, PID: 5019
              android.os.FileUriExposedException: file:///storage/emulated/0/My%20Children/2018-19-09.jpg exposed beyond app through ClipData.Item.getUri()
                  at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
                  at android.net.Uri.checkFileUriExposed(Uri.java:2346)
                  at android.content.ClipData.prepareToLeaveProcess(ClipData.java:832)
                  at android.content.Intent.prepareToLeaveProcess(Intent.java:8909)
                  at android.content.Intent.prepareToLeaveProcess(Intent.java:8894)
                  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1517)
                  at android.app.Activity.startActivityForResult(Activity.java:4224)
                  at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
                  at android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:152)
                  at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:798)
                  at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:907)
                  at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:1028)
                  at android.support.v4.app.Fragment.startActivityForResult(Fragment.java:1017)
                  at com.smiles.mychildren.Images$1.onClick(Images.java:98)
                  at android.view.View.performClick(View.java:5609)
                  at android.view.View$PerformClick.run(View.java:22259)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

共有1个答案

颛孙英才
2023-03-14

您在这里做错了,您在调用StartActivityForResult之后添加了参数。

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriImage);

把这个换掉

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriImage);
startActivityForResult(intent, TAKE_PICTURE);

同时,您为图像捕获和从图库中选择图像设置了相同的请求代码,您必须设置不同的请求代码

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == TAKE_PICTURE &&  resultCode== RESULT_OK){
            Bitmap bitmap = (Bitmap) data.getExtras().get("data");
            Intent i = new Intent(getActivity(), Upload.class);
            i.putExtra("image", bitmap);
            startActivity(i);
    }

  //TAKE_PICTURE(int 1) request code for camera capture
 //PICK_PICTURE_FROM_GALLERY (int 2) code for select image from gallery.

   if (requestCode == PICK_PICTURE_FROM_GALLERY &&  resultCode== RESULT_OK){
            Bitmap bitmap = (Bitmap) data.getExtras().get("data");
            Intent i = new Intent(getActivity(), Upload.class);
            i.putExtra("image", bitmap);
            startActivity(i);
    }
  }
 类似资料:
  • 在“开发者选项”设置中,我使用“不保留活动”模式。我需要从画廊选择图像,并在我的应用程序中使用它的URI。打开图库以选取图像的代码是: 当我选择图像时发生问题。“请等待...”显示消息,然后我返回图库中的“选择图像”。打开此模式时,HTC One X设备会发生这种情况。在三星Galaxy S3中不会出现此问题。当此模式打开时,如何在HTC ONE X设备上解决它?

  • 用户可以从图库中捕获或选择图像。我想把那个图像上传到服务器上。 这是我的相机密码- 这是我从图库中选择图像的代码- 另外,我用下面的代码显示图像- 现在,我想把这个图像转换成base64并上传到服务器。

  • 编辑:我调试了应用程序,并用初始化了。这消除了错误,但是现在ImageView没有得到更新,但是当我从Gallery中选择image时,它已经更新了。

  • 我在drawable文件夹(example.jpg,sue.png)中有2个图像。 我想打开drawable文件夹作为一个库来选择一个新的图像,然后新的图像将替换原始图像(在本例中,原始图像是example.jpg) 但是当我打开图库选择新图像时,却没有图像可供选择。 我应该如何解决这个问题? 下面是我的代码: ProfileActivity: XML:

  • 有没有一个标准的方法来调用对话框选择从相机或从图库(如内置电话簿或Skype)的图像? 我看了一下这个,但是代码打开了图库,没有建议从相机中选择它。 设备:Samsung Galaxy Tab Android:2.3.3

  • 图库和相机在调试中正确打开(使用模拟器和真实设备),软件包请求权限(我在android清单中设置了它们),并在接受它们后打开图库。没有警告/错误。当我使用发布应用程序并在我的手机上测试它(小米redmi Note 7)时,图像选择器不会打开图库。按钮不做任何事情,应用程序也不会崩溃。我没有尝试,但我打赌它会起作用。正如我所说,我有image_picker的最新版本api 29的权限,并尝试用flu