How to get access to the full-size photo after Uri after save
225 观看
1回复
98 作者的声誉
As documentation describe here Documentation I'm trying to get access to full-size image save by intent. I have followed every step in the documentation as I understand still. Now I'm wondering how to get access to that file onActivityResult()
. because Intent
on that method is null.
Activity.java
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
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.chathurangashan.cameraintent",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
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
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
if (resultCode == RESULT_OK) {
if (data.hasExtra(MediaStore.EXTRA_OUTPUT)) {
Uri uri = (Uri) data.getParcelableExtra(MediaStore.EXTRA_OUTPUT);
}
}
}
}
AndroidManifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chathurangashan.cameraintent">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.chathurangashan.cameraintent"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
file_path.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.chathurangashan.cameraintent/files/Pictures" />
</paths>
file_path.xml is save under resource folder in 'res'
PS: I know I can get the bit map in onActivityResult()
directly as documentation describe under "Get the thumbnail" but it's not returning full size image. it's a low quality version of the captured image.
回应 1
0像
4560 作者的声誉
your dispatchTakePictureIntent() should return an intent so that you can get Uri path as extra, back using MediaStore.EXTRA_OUTPUT
as key.
private Intent dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
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.chathurangashan.cameraintent",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
}
}
return takePictureIntent;
}
now you should start your intent like this and store it somewhere globally to be used later.
intentImage = dispatchTakePictureIntent();
startActivityForResult(intentImage, REQUEST_IMAGE_CAPTURE);
now in your onActivityResult do something like this.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
if (resultCode == RESULT_OK) {
if (intentImage.hasExtra(MediaStore.EXTRA_OUTPUT)) {
//your uri from saved intent
Uri uri = Uri.parse(intentImage.getStringExtra(MediaStore.EXTRA_OUTPUT));
}
}
}
}
作者: vikas kumar
发布者: 2017 年 12 月 27 日
来自类别的问题 :
- android Android Phone Emulator如何反映性能?
- android 如何使用保存实例状态保存Android Activity状态?
- android Android:从ListView访问子视图
- android 如何在Android上调用SOAP Web服务
- android 如何在Android中格式化日期和时间?
- android Android:从相机获取文件名?
- android 在Android上旋转活动重启
- android 将图像加载到Bitmap对象时出现奇怪的内存不足问题
- android 使用数据库发送应用程序
- android 如何在android中以编程方式删除联系人
- android-camera 检查设备是否有摄像头?
- android-camera 从相机拍摄照片无需预览
- android-camera Android:无法从相机获取RAW图像数据?
- android-camera 将位图数组转换为YUV(YCbCr NV21)
- android-camera 在Android Camera Intent上按下后退按钮时应用程序崩溃
- android-camera Android相机预览回调和mediarecorder录制视频
- android-camera 在Android中的onPreviewFrame期间转换YUV-> RGB(图像处理)-> YUV?
- android-camera 如何有目的地推出前置摄像头?
- android-camera Android Camera:数据意图返回null
- android-camera 通过意图拍照后调用的活动已被杀死/ onCreate