【问题标题】:Cannot upload the image taken directly from camera无法上传直接从相机拍摄的图像
【发布时间】:2019-07-16 20:07:43
【问题描述】:

您好,我在直接从相机上传图片时遇到问题。错误是:

/mnt/sdcard/Phoenix/default: open failed: ENOENT (No such file or directory)

我尝试了许多其他方法,但我面临同样的问题,即没有这样的文件或目录。 我的代码如下:

if (options[item].equals("Take Photo")) {
                        dialog.dismiss();
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                        startActivityForResult(intent, PICK_IMAGE_CAMERA);
                    }

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        inputStreamImg = null;
        if (requestCode == PICK_IMAGE_CAMERA) {
            try {

                File f = new File(Environment.getExternalStorageDirectory().toString());
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.jpg")) {
                        f = temp;
                        break;
                    }
                }
                try {
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                    bitmapPrescribedMed = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
                    imgPrescribedMedicinePath = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "Phoenix" + File.separator + "default";
                    f.delete();
                    OutputStream outFile = null;
                    File file = new File(imgPrescribedMedicinePath, String.valueOf(System.currentTimeMillis()) + ".jpg");
                    try {
                        outFile = new FileOutputStream(file);
                        bitmapPrescribedMed.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                        outFile.flush();
                        outFile.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

有人可以帮帮我吗?

【问题讨论】:

  • 你有存储访问权限吗?
  • 检查默认文件夹是否存在,并验证您从哪里获取要上传的图像文件的路径
  • 请指定测试设备和相应设备的Android API级别。
  • @Ashishsingh 是的,我已获得存储权限
  • @iCantC 我正在使用的设备的 api 级别是 17

标签: android android-camera


【解决方案1】:

我们实际上将文件写入外部存储。尝试在您的 AndroidManifest 中添加以下内容。

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

如果这不起作用尝试多次重现,如果该工作一次然后每次都工作它可能会说模块需要在自定义本地目录中创建图像

【讨论】:

  • 另外,需要从Android 6开始申请运行时权限
猜你喜欢
  • 1970-01-01
  • 2021-11-06
  • 2011-07-15
  • 1970-01-01
  • 2017-03-07
  • 2017-06-02
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
相关资源
最近更新 更多