[英]intent value null in onActivityResult method of the fragment
我開始使用文件提供程序捕獲凸輪圖像。 我的片段根據與簡單拍攝圖像有關的android文檔調用cam capture intent。
問題在於onActivityResult
方法意圖返回null。 這是我設置所有東西的方式。 我無法弄清楚我在做什么錯。
AndroidManifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path" />
</provider>
file_path.xml
<paths>
<external-files-path name="my_images" />
</paths>
Fragment.class中關於捕獲意圖的實現
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
try {
photoFile = Utils.createImageFile(getContext());
String authorities = context.getPackageName()+".fileprovider";
uriForFile = FileProvider.getUriForFile(context, authorities, photoFile);
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "handleCaptureImage: "+uriForFile); // shows this result in logs content://appid.fileprovider/my_images/Pictures/JPEG_20170918_181145_719404055.jpg
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
startActivityForResult(intent, OPEN_CAMERA);
片段中的onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == PICK_IMAGE) {
try {
//works fine bitmap generated correctly.
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
} else if (requestCode == OPEN_CAMERA) {
Log.d(TAG, "onActivityResult: "+data); //data always null
}
}
}
我嘗試在托管活動中調用onActivityResult(),但結果相同。 需要正確的方向指針。
這將對onActivityResult中的數據有幫助
如果data.getData()返回null,請嘗試:
Bundle extras = data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
希望有幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.