24

Fragment.

如果我使用文件中的 Uri Uri.fromFile(file)

使用File String, 的形式'/data/data/com.package.bla/cache/img198346262jpg',它不能正常工作(文件在那里,但它是空的,因为相机没有在上面保存任何东西)。

到目前为止我尝试了什么:

  • 在创建文件后删除文件,如本例所示。但是,相机退出后该文件不存在。
  • 添加了外部存储读取权限,以防万一

所以我不知道为什么图像没有被保存并且已经花费/浪费了大量时间测试和弄清楚它为什么不起作用。

分段:

private void launchCamera() {
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    File outputDir = getActivity().getCacheDir();
    File file = null;
    try {
        file = File.createTempFile("img", "jpg", outputDir);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (file != null) {
        mImageUri = Uri.fromFile(file);   //using Uri is not even exiting the camera
        //mImageUri = File.toString();    //If I use String instead of an Uri, it works better (ie, can accept camera photo)
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
        startActivityForResult(cameraIntent, RESULT_TAKE_IMAGE);
    }
}


public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        Bitmap original = BitmapFactory.decodeFile(mImageUri.toString(), bounds);
    }
}

编辑后的代码,mImageUri。如前所述,如果我使用 Uri,我什至无法接受相机应用程序中的照片。使用字符串可以让我接受照片,尽管照片实际上并没有保存(即文件里面有 0 个字节)。

解释:问题与保存到缓存目录有关。也许这是一个错误,我缺少权限,或者相机应用程序无法保存到您的应用程序私有数据目录中。添加权限 FLAG_GRANT_WRITE_URI_PERMISSION 并没有解决它。相关文章:将相机中的图像存储到私有应用程序缓存目录中并在通过意图启动时将相机数据保存到缓存中

从 Android 2.2 开始的更新getExternalCacheDir()方法可以用来代替getCacheDir()

4

3 回答 3

7

从 android 26+ Uri.fromFile 将不起作用,您应该改用 File 提供程序。

AndroidManifest.xml

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

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

res/xml/file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external"
        path="." />
</paths>

最后

final Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

// output file
File path = new File(Environment.getExternalStorageDirectory(), "tmp.mp4");

// com.mydomain.fileprovider is authorities (manifest)
// getUri from file
Uri uri = FileProvider.getUriForFile(this, "com.mydomain.fileprovider", path);

takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(takeVideoIntent, 99);

在 android 8.0 和 5.1.1 上测试

更新:在某些设备上,内置摄像头不支持 EXTRA_OUTPUT,因此如果您想在所有设备上工作,请构建自己的摄像头模块。

于 2018-05-31T03:51:37.447 回答
4

为什么不将其保存在新文件中

    final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
    root.mkdirs();
    final String fname = "img_"+ System.currentTimeMillis() + ".jpg";
    final File sdImageMainDirectory = new File(root, fname);
    mImageUri = Uri.fromFile(sdImageMainDirectory);

然后将该uri传递给意图

    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
于 2015-09-03T18:40:51.207 回答
0

试试这对我来说就像魅力一样

private String selectedImagePath = "";
    final private int PICK_IMAGE = 1;
    final private int CAPTURE_IMAGE = 2;

public Uri setImageUri() {
        // Store image in dcim
        File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
        Uri imgUri = Uri.fromFile(file);
        this.imgPath = file.getAbsolutePath();
        return imgUri;
    }


    public String getImagePath() {
        return imgPath;
    }

btnGallery.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);

            }
        });

        btnCapture.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
                startActivityForResult(intent, CAPTURE_IMAGE);
            }
        });

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == PICK_IMAGE) {
                selectedImagePath = getAbsolutePath(data.getData());
                imgUser.setImageBitmap(decodeFile(selectedImagePath));
            } else if (requestCode == CAPTURE_IMAGE) {
                selectedImagePath = getImagePath();
                imgUser.setImageBitmap(decodeFile(selectedImagePath));
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

    }


public Bitmap decodeFile(String path) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, o);
            // The new size we want to scale to
            final int REQUIRED_SIZE = 70;

            // Find the correct scale value. It should be the power of 2.
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
                scale *= 2;

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeFile(path, o2);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;

    }

public String getAbsolutePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }
于 2013-09-10T05:54:42.670 回答