如何将相机结果作为数据文件夹中的uri



我正在创建一个应用程序,我想在其中捕获图像,然后我想将该图像作为附件发送到电子邮件中。

我正在使用android.provider.MediaStore.ACTION_IMAGE_CAPTURE意图动作打开相机,我正在传递文件的Uri作为参数EXTRA_OUTPUT,以将图像返回到文件。这是完美的工作,我能够获得捕获的图像,如果我使用external storage uri作为EXTRA_OUTPUT,但如果我使用数据文件夹uri,它不工作,相机不关闭,其所有按钮都不工作。

下面是我在外部存储目录 中获取结果的代码
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File out = Environment.getExternalStorageDirectory();
out = new File(out, imagename);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(i, CAMERA_RESULT);

这段代码用于获取data文件夹

中的图像
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File out = getFilesDir();
out = new File(out, MyPharmacyOptions.PRESCRIPTION_IMAGE_NAME);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(i, CAMERA_RESULT);

我知道第三个应用程序无法访问数据文件夹所以这可能会导致一个问题,所以我已经创建了一个内容提供程序来共享文件。

这里是我的内容提供类

public class MyContentProvider extends ContentProvider {
private static final String Tag = RingtonContentProvider.class.getName();
public static final Uri CONTENT_URI = Uri
.parse("content://x.y.z/");
private static final HashMap<String, String> MIME_TYPES = new HashMap<String, String>();
static {
MIME_TYPES.put(".mp3", "audio/mp3");
MIME_TYPES.put(".wav", "audio/mp3");
MIME_TYPES.put(".jpg", "image/jpeg");
}
@Override
public boolean onCreate() {
return true;
}
@Override
public String getType(Uri uri) {
String path = uri.toString();
for (String extension : MIME_TYPES.keySet()) {
if (path.endsWith(extension)) {
return (MIME_TYPES.get(extension));
}
}
return (null);
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
File f = new File(getContext().getFilesDir(), uri.getPath());
if (f.exists()) {
return (ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY));
}
throw new FileNotFoundException(uri.getPath());
}
@Override
public Cursor query(Uri url, String[] projection, String selection,
String[] selectionArgs, String sort) {
throw new RuntimeException("Operation not supported");
}
@Override
public Uri insert(Uri uri, ContentValues initialValues) {
File file = new File(getContext().getFilesDir(), uri.getPath());
if(file.exists()) file.delete();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Uri.fromFile(file);
}
@Override
public int update(Uri uri, ContentValues values, String where,
String[] whereArgs) {
throw new RuntimeException("Operation not supported");
}
@Override
public int delete(Uri uri, String where, String[] whereArgs) {
File f = new File(getContext().getFilesDir(), "image1.jpg");
if(f.exists()) f.delete();
f = new File(getContext().getFilesDir(), "image2.jpg");
if(f.exists()) f.delete();
getContext().getContentResolver().notifyChange(CONTENT_URI, null);
}
}

要使用这个内容提供,我使用以下代码将uri传递给相机活动

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = MyContentProvider.CONTENT_URI;
uri = Uri.withAppendedPath(uri, imagename);
getContentResolver().insert(uri, null);
getContentResolver().notifyChange(RingtonContentProvider.CONTENT_URI, null);
Log.d(Tag, uri.toString());
i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(i, CAMERA_RESULT);

现在,如果我传递外部存储目录以外的url,相机正在打开,但在模拟器中没有关闭,但在设备中,相机将关闭,但我没有得到结果。

我已经在manifest文件中声明了这个内容

<provider
android:name=".contentproviders.MyContentProvider"
android:authorities="x.y.z" />

还允许写入外部存储对于也使用camera.

我能够使用外部存储捕获图像,但我希望将图像存储在数据目录中,而不是外部存储,因为如果外部存储不可用,我想捕获图像并想发送邮件。

如果我创建内容,那么我也可以将我的图像共享到电子邮件应用程序。

如果我们不提供额外的相机意图,它将返回图像作为一个字节[]在活动结果作为一个额外的数据,但这是缩略图的目的,所以我不能得到高分辨率的图像使用这种方式。

解决这个问题有两种方法。

1。保存从onActivityResult方法接收的位图

你可以使用下面的代码启动相机意图捕捉照片

Intent cameraIntent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

捕获照片后,您将在onActivityResult方法中获得位图

if (requestCode == CAMERA_REQUEST) {  
Bitmap photo = (Bitmap) data.getExtras().get("data"); 
}

现在你可以简单地将这个位图保存到内部存储器

注意:这里位图对象由拇指图像组成,它不会有全分辨率图像

2。使用内容提供程序直接将位图保存到内部存储器

在这里,我们将创建内容提供程序类,以允许本地存储目录对相机活动的权限

提供程序示例如下

public class MyFileContentProvider extends ContentProvider {
public static final Uri CONTENT_URI = Uri.parse
("content://com.example.camerademo/");
private static final HashMap<String, String> MIME_TYPES = 
new HashMap<String, String>();
static {
MIME_TYPES.put(".jpg", "image/jpeg");
MIME_TYPES.put(".jpeg", "image/jpeg");
}
@Override
public boolean onCreate() {
try {
File mFile = new File(getContext().getFilesDir(), "newImage.jpg");
if(!mFile.exists()) {
mFile.createNewFile();
}
getContext().getContentResolver().notifyChange(CONTENT_URI, null);
return (true);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
public String getType(Uri uri) {
String path = uri.toString();
for (String extension : MIME_TYPES.keySet()) {
if (path.endsWith(extension)) {
return (MIME_TYPES.get(extension));
}
}
return (null);
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
File f = new File(getContext().getFilesDir(), "newImage.jpg");
if (f.exists()) {
return (ParcelFileDescriptor.open(f,
ParcelFileDescriptor.MODE_READ_WRITE));
}
throw new FileNotFoundException(uri.getPath());
}
}

之后,您可以使用下面的代码

简单地使用URI传递到相机活动
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI);
startActivityForResult(i, CAMERA_RESULT);

如果你不想创建自己的提供商,那么你可以使用support-library-v4中的FileProvider。要获得详细的帮助,您可以查看这篇文章

我发现的最佳解决方案是:FileProvider(需要support-library-v4)它使用内部存储!https://developer.android.com/reference/android/support/v4/content/FileProvider.html

  1. 在Manifest in Application元素中定义你的FileProvider:

    <provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="your.package.name.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true" >
    <meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/image_path" />
    </provider>
    
  2. 在manifest根元素中添加权限:

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" android:required="false" />
    
  3. 定义您的图像路径,例如res/xml/image_path.xml:

    <paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="captured_image" path="your/path/"/>
    </paths>
    
  4. Java:
  5. private static final int IMAGE_REQUEST_CODE = 1;
    // your authority, must be the same as in your manifest file 
    private static final String CAPTURE_IMAGE_FILE_PROVIDER = "your.package.name.fileprovider";
    

4.1 capture intent:

File path = new File(activity.getFilesDir(), "your/path");
if (!path.exists()) path.mkdirs();
File image = new File(path, "image.jpg");
Uri imageUri = FileProvider.getUriForFile(activity, CAPTURE_IMAGE_FILE_PROVIDER, image);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, IMAGE_REQUEST_CODE);

4.2 onActivityResult ():

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == IMAGE_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
File path = new File(getFilesDir(), "your/path");
if (!path.exists()) path.mkdirs();
File imageFile = new File(path, "image.jpg");
// use imageFile to open your image
}
}
super.onActivityResult(requestCode, resultCode, intent);
}

意图调用捕捉照片,

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
startActivityForResult(cameraIntent, CAMERA_REQUEST);  

然后在ActivityResult中取位图

if (requestCode == CAMERA_REQUEST) {   
Bitmap photo = (Bitmap) data.getExtras().get("data");  
}   

然后写入内存,参见

// The openfileOutput() method creates a file on the phone/internal storage in the context of your application  
final FileOutputStream fos = openFileOutput("my_new_image.jpg", Context.MODE_PRIVATE); 
// Use the compress method on the BitMap object to write image to the OutputStream 
bm.compress(CompressFormat.JPEG, 90, fos); 

那么下次读取该文件时,

Bitmap bitmap = BitmapFactory.decodeFile(file); 

首先保存照片在您的外部存储和尝试-

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, CAMERA_REQUEST); 
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
if (requestCode == CAMERA_REQUEST) {  
Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray(); // convert camera photo to byte array
// save it in your external storage.
FileOutputStream fo = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/_camera.png"));
fo.write(byteArray);
fo.flush();
fo.close();
}  
} 

下一个目标-

File cameraFile = new File(Environment.getExternalStorageDirectory() + "/_camera.png");                 
startActivityForResult(Intent.createChooser(new Intent(Intent.ACTION_SEND)
.setType("image/jpg")
.putExtra(Intent.EXTRA_SUBJECT, "Subject")
.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cameraFile))
.putExtra(Intent.EXTRA_TEXT, textBody), "Send your message using"), Constant.EMAIL);

你也可以做到这一点,而不需要一个内容提供程序,因为你将需要sd卡打开相机图像捕获意图。当然,您可以破解sd卡的存在,但不能使用相机意图捕获....因此,您正在检查外部存储,但此时需要它....仅供参考,你还应该检查像crop-image这样的裁剪库,而不是使用native,因为它不是跨设备很好地支持。

File mediaStorageDir;
String photoFileName = "photo.jpg";

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// page = getArguments().getInt("someInt", 0);
// title = getArguments().getString("someTitle");
// Get safe storage directory for photos
mediaStorageDir = new File(
Environment                          .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
APP_TAG);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()) {
Log.d(APP_TAG, "Directory exists: " + mediaStorageDir.isDirectory());
Log.d(APP_TAG, "Directory exists: " + mediaStorageDir.getPath());
Log.d(APP_TAG,
"Directory exists: "
+ Environment.getExternalStorageState());
Log.d(APP_TAG, "failed to create directory");
}
}

在你的'take the pic'代码中:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName));

public Uri getPhotoFileUri(String fileName) {
return Uri.fromFile(new File(mediaStorageDir.getPath() + File.separator
+ fileName));
}

在研究这个bug一段时间后,我注意到调用相机意图的活动只有在手机内存耗尽时才会重新启动。因此,由于活动被重新启动,保存捕获图像的路径或Uri的对象被刷新(null)

所以我建议你在onActivityResult中捕获/检测null对象,并提示用户释放设备上的空间或重新启动手机以获得快速的临时修复。

最新更新