onActivityResult返回Intent data.getData();总是Null只在棉花糖和棒棒糖 [英] onActivityResult returning Intent data.getData(); always Null Only in Marshmallow and Lollipop
问题描述
阅读我的整个代码。它在除 Marshmallow
和 Lollipop
之外的每个电话上都能正常工作。 Marshmallow
和 Lollipop
手机只有问题出现在 data.getData()
仅在捕获的图片的情况下返回 null
。如果我捕获视频,它工作正常。我有4个 -
-
并在
Imageview
中显示缩略图。 -
从相机捕获图片并显示其缩略图
Marshmallow
和Lollipop $>中的
Imageview
- 不工作 c $ c> -
从图库中选择视频,并在
Imageview
-
3.从相机捕获视频,并在
Imageview中显示其缩略图
- 工作精细。 >
现在有很多解决方案,但是他们都不满意,如果代码在视频捕获的情况下正常工作,为图像捕获工作?它是一个错误或我在某个地方做错了?
这里是我的代码。我已评论了发生崩溃的行 -
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
int THUMBSIZE = 120;
switch(requestCode){
case RESULT_LOAD_IMAGE:
if(resultCode == RESULT_OK){
String column_Name = MediaStore.Images.Media.DATA;
String picturePath = getPath(column_Name,data.getData());
Bitmap orientedBitmap = ExifUtil.rotateBitmap(picturePath,BitmapFactory.decodeFile(picturePath));
Drawable fullpic = new BitmapDrawable(getResources(),ThumbnailUtils.extractThumbnail(orientedBitmap,THUMBSIZE,THUMBSIZE));
thumbnailview.setBackground(fullpic);
editText.setText(picturePath);
picker.setVisibility(View.VISIBLE);
thumbnailview.setClickable(false);
}
break;
case REQUEST_IMAGE_CAPTURE:
if(resultCode == RESULT_OK){
String picturePath =;
String column_Name = MediaStore.Images.Media.DATA;
picturePath = getPath(column_Name,data.getData());
//我的应用程序崩溃,因为在Marshmallow data.getData()总是null。
位图orientationBitmap = ExifUtil.rotateBitmap(picturePath,BitmapFactory.decodeFile(picturePath));
Drawable fullpic = new BitmapDrawable(getResources(),ThumbnailUtils.extractThumbnail(orientedBitmap,THUMBSIZE,THUMBSIZE));
thumbnailview.setBackground(fullpic);
editText.setText(picturePath);
picker.setVisibility(View.VISIBLE);
thumbnailview.setClickable(false);
}
break;
case RESULT_LOAD_VIDEO:
if(resultCode == RESULT_OK){
String column_Name = MediaStore.Video.Media.DATA;
String videoPath = getPath(column_Name,data.getData());
Drawable fullpic = new BitmapDrawable(getResources(),ThumbnailUtils.createVideoThumbnail(videoPath,MediaStore.Video.Thumbnails.MINI_KIND));
thumbnailview.setBackground(fullpic);
editText.setText(videoPath);
picker.setVisibility(View.VISIBLE);
thumbnailview.setClickable(false);
}
break;
case REQUEST_VIDEO_CAPTURE:
if(resultCode == RESULT_OK){
String column_Name = MediaStore.Video.Media.DATA;
String videoPath = getPath(column_Name,data.getData());
Drawable fullpic = new BitmapDrawable(getResources(),ThumbnailUtils.createVideoThumbnail(videoPath,MediaStore.Video.Thumbnails.MINI_KIND));
thumbnailview.setBackground(fullpic);
editText.setText(videoPath);
picker.setVisibility(View.VISIBLE);
thumbnailview.setClickable(false);
}
break;
}
if(picker!= null){
picker.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick View v){
OpenDialog();
}
});
}
}
private String getPath(String column_Name,Uri uri){
String [] projection = {column_Name};
String path =;
Cursor cursor = getContentResolver()。query(uri,projection,null,null,null);
int column_index_data;
if(cursor!= null){
column_index_data = cursor.getColumnIndexOrThrow(column_Name);
cursor.moveToFirst();
path = cursor.getString(column_index_data);
cursor.close();
}
返回路径;
}
private void OpenDialog(){
dialogBox.setTitle(Select an Action);
dialogBox.setMessage(选择图片或视频);
dialogBox.setPositiveButton(Picture,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
if check_Permissions()){
OpenCameraDialog();
}
else {
request_Permissions();
CAMERA_DIALOG_PERMISSION = 1;
} b $ b}
});
dialogBox.setNegativeButton(Video,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
if check_Permissions()){
OpenVideoDialog();
}
else {
request_Permissions();
VIDEO_DIALOG_PERMISSION = 1;
}
b $ b}
});
dialogBox.show();
}
private void OpenCameraDialog(){
dialogBox.setTitle(Select an Action);
dialogBox.setMessage(Choose Picture From);
dialogBox.setPositiveButton(Gallery,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
final Intent galleryintent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryintent,RESULT_LOAD_IMAGE);
}
});
dialogBox.setNegativeButton(Camera,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
final Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE);
}
});
dialogBox.show();
}
private void OpenVideoDialog(){
dialogBox.setTitle(Select an Action);
dialogBox.setMessage(Choose Video From);
dialogBox.setPositiveButton(Gallery,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
final Intent galleryintent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryitent,RESULT_LOAD_VIDEO);
}
});
dialogBox.setNegativeButton(Camera,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
final Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if(takeVideoIntent.resolveActivity(getPackageManager())!= null){
startActivityForResult(takeVideoIntent,REQUEST_VIDEO_CAPTURE);
}
}
});
dialogBox.show();
}
private boolean check_Permissions(){
boolean GRANTED;
if(ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)+
ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE)+
ContextCompat.checkSelfPermission Manifest.permission.RECORD_AUDIO)+
ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
GRANTED = false;
}
else {
GRANTED = true;
}
return GRANTED;
}
private void request_Permissions(){
ActivityCompat.requestPermissions(this,
new String [] {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO},
REQUEST_FOR_PERMISSION);
}
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String [] permissions,@NonNull int [] grantResults){
super .onRequestPermissionsResult(requestCode,permissions,grantResults);
switch(requestCode){
case REQUEST_FOR_PERMISSION:
if((grantResults.length> 0)&&(grantResults [0] + grantResults [1] ] + grantResults [2] + grantResults [3] == PackageManager.PERMISSION_GRANTED)){
if(CAMERA_DIALOG_PERMISSION == 1){
OpenCameraDialog
CAMERA_DIALOG_PERMISSION = 0;
}
else if(VIDEO_DIALOG_PERMISSION == 1){
OpenVideoDialog();
VIDEO_DIALOG_PERMISSION = 0;
}
}
else {
Toast.makeText(this,请GRANT权限,Toast.LENGTH_SHORT).show
}
}
}
不,它不是。在许多Android版本上,当用户选择一个编写好的相机应用来处理您的 ACTION_IMAGE_CAPTURE
请求时,它会失败。
您的问题在这里:
if(data.getData()!= null){
picturePath = getPath(column_Name,data.getData());}
else {
//我的应用程序崩溃,因为在Marshmallow data.getData()总是空。
}
这里至少有两个缺陷。
大的假设你从 ACTION_IMAGE_CAPTURE
返回 Uri
。 没有记录,相机应用不需要返回a Uri
。特别是,在你的结构中,你只能通过 getExtra(data)
获得缩略图。如果你想要一个全尺寸的图像,在 Intent
上使用 EXTRA_OUTPUT
,在这种情况下,你知道图像存储它是在 EXTRA_OUTPUT
中指示的任何位置。
另一个是你假设 Uri
,您返回的是 MediaStore
,否则有 MediaStore.Video.Media.DATA
列。相机应用程序不仅不必返回
Uri
,而且不需要 Uri
来自 MediaStore
或有这样的列。
Read My Whole Code. It is working perfectly on every Phone except Marshmallow
and Lollipop
. In Marshmallow
and Lollipop
phones Only problem is coming in data.getData()
returning null
only in case of Picture Captured. It is working fine if i capture Video .I have 4 cases -
Choosing Picture from Gallery and show its thumbnail in
Imageview
- Working Fine.Capturing Picture from Camera and show its thumbnail in
Imageview
- NOT WORKING inMarshmallow
andLollipop
.Choosing Video from Gallery and show its thumbnail in
Imageview
- Working Fine.3.Capturing Video from Camera and show its thumbnail in
Imageview
- Working Fine.
Now There are many solutions but neither of them looks satisfactory,If code is working fine in case of Video Capturing then why its NOT working for Image Capturing?Is it a Bug or I am doing wrong somewhere?
Here is My Code . I have commented a line where crash happens-
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
int THUMBSIZE = 120;
switch (requestCode){
case RESULT_LOAD_IMAGE:
if (resultCode==RESULT_OK){
String column_Name= MediaStore.Images.Media.DATA;
String picturePath=getPath(column_Name,data.getData());
Bitmap orientedBitmap = ExifUtil.rotateBitmap(picturePath, BitmapFactory.decodeFile(picturePath));
Drawable fullpic=new BitmapDrawable(getResources(),ThumbnailUtils.extractThumbnail(orientedBitmap, THUMBSIZE, THUMBSIZE));
thumbnailview.setBackground(fullpic);
editText.setText(picturePath);
picker.setVisibility(View.VISIBLE);
thumbnailview.setClickable(false);
}
break;
case REQUEST_IMAGE_CAPTURE:
if (resultCode==RESULT_OK){
String picturePath="";
String column_Name=MediaStore.Images.Media.DATA;
picturePath=getPath(column_Name,data.getData());
//My app Crashes here because in Marshmallow data.getData() is always null.
Bitmap orientedBitmap = ExifUtil.rotateBitmap(picturePath, BitmapFactory.decodeFile(picturePath));
Drawable fullpic=new BitmapDrawable(getResources(),ThumbnailUtils.extractThumbnail(orientedBitmap, THUMBSIZE, THUMBSIZE));
thumbnailview.setBackground(fullpic);
editText.setText(picturePath);
picker.setVisibility(View.VISIBLE);
thumbnailview.setClickable(false);
}
break;
case RESULT_LOAD_VIDEO:
if (resultCode==RESULT_OK){
String column_Name=MediaStore.Video.Media.DATA;
String videoPath=getPath(column_Name,data.getData());
Drawable fullpic=new BitmapDrawable(getResources(),ThumbnailUtils.createVideoThumbnail(videoPath,MediaStore.Video.Thumbnails.MINI_KIND));
thumbnailview.setBackground(fullpic);
editText.setText(videoPath);
picker.setVisibility(View.VISIBLE);
thumbnailview.setClickable(false);
}
break;
case REQUEST_VIDEO_CAPTURE:
if (resultCode==RESULT_OK){
String column_Name=MediaStore.Video.Media.DATA;
String videoPath=getPath(column_Name,data.getData());
Drawable fullpic=new BitmapDrawable(getResources(),ThumbnailUtils.createVideoThumbnail(videoPath,MediaStore.Video.Thumbnails.MINI_KIND));
thumbnailview.setBackground(fullpic);
editText.setText(videoPath);
picker.setVisibility(View.VISIBLE);
thumbnailview.setClickable(false);
}
break;
}
if (picker != null) {
picker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OpenDialog();
}
});
}
}
private String getPath(String column_Name,Uri uri){
String[] projection = {column_Name};
String path = "";
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index_data ;
if (cursor != null) {
column_index_data = cursor.getColumnIndexOrThrow(column_Name);
cursor.moveToFirst();
path = cursor.getString(column_index_data);
cursor.close();
}
return path;
}
private void OpenDialog(){
dialogBox.setTitle("Select an Action");
dialogBox.setMessage("Choose Picture or Video");
dialogBox.setPositiveButton("Picture", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (check_Permissions()){
OpenCameraDialog();
}
else {
request_Permissions();
CAMERA_DIALOG_PERMISSION=1;
}
}
});
dialogBox.setNegativeButton("Video", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (check_Permissions()){
OpenVideoDialog();
}
else {
request_Permissions();
VIDEO_DIALOG_PERMISSION=1;
}
}
});
dialogBox.show();
}
private void OpenCameraDialog(){
dialogBox.setTitle("Select an Action");
dialogBox.setMessage("Choose Picture From");
dialogBox.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final Intent galleryintent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryintent, RESULT_LOAD_IMAGE);
}
});
dialogBox.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
});
dialogBox.show();
}
private void OpenVideoDialog(){
dialogBox.setTitle("Select an Action");
dialogBox.setMessage("Choose Video From");
dialogBox.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final Intent galleryintent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryintent, RESULT_LOAD_VIDEO);
}
});
dialogBox.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
});
dialogBox.show();
}
private boolean check_Permissions(){
boolean GRANTED;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) +
ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE) +
ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO) +
ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
GRANTED=false;
}
else {
GRANTED=true;
}
return GRANTED;
}
private void request_Permissions(){
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO},
REQUEST_FOR_PERMISSION);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case REQUEST_FOR_PERMISSION:
if ((grantResults.length>0)&& (grantResults[0] +grantResults[1]+grantResults[2]+grantResults[3]== PackageManager.PERMISSION_GRANTED)){
if (CAMERA_DIALOG_PERMISSION==1){
OpenCameraDialog();
CAMERA_DIALOG_PERMISSION=0;
}
else if (VIDEO_DIALOG_PERMISSION==1){
OpenVideoDialog();
VIDEO_DIALOG_PERMISSION=0;
}
}
else {
Toast.makeText(this, "Please GRANT Permissions", Toast.LENGTH_SHORT).show();
}
}
}
It is working perfectly on every Phone except Marshmallow and Lollipop
No, it is not. It will fail on lots of Android versions, when the user chooses a well-written camera app to handle your ACTION_IMAGE_CAPTURE
request.
Your problem lies here:
if (data.getData()!=null){
picturePath=getPath(column_Name,data.getData());}
else {
//My app Crashes here because in Marshmallow data.getData() is always null.
}
There are at least two flaws here.
The big one is assuming that you get a Uri
back from ACTION_IMAGE_CAPTURE
. That is not documented, and camera apps do not need to return a Uri
. In particular, in your structure, you will only get a thumbnail back, via getExtra("data")
. If you want a full-size image, use EXTRA_OUTPUT
on your Intent
, in which case you know where the image is being stored — it is wherever you are indicating in EXTRA_OUTPUT
.
The other one is that you assume that the Uri
that you get back is from the MediaStore
or otherwise has a MediaStore.Video.Media.DATA
column. Not only do camera apps not have to return a Uri
, but there is no requirement that the Uri
is from the MediaStore
or have such a column.
这篇关于onActivityResult返回Intent data.getData();总是Null只在棉花糖和棒棒糖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!