startActivityForResult 返回数据 null
Posted
技术标签:
【中文标题】startActivityForResult 返回数据 null【英文标题】:startActivityForResult return data null 【发布时间】:2016-12-22 18:47:00 【问题描述】:我尝试从相机发送图像到我的活动。我像这样从adapter
打电话给startActivityForResult
:
photo.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
dialog.dismiss();
mActivity.dispatchTakePictureIntent(part.getId());
);
这是 Activity 上的那个方法:
public void dispatchTakePictureIntent(String id)
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.eltegps011.eltegps.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
takePictureIntent.putExtra("Id",id);
startActivityForResult(takePictureIntent, CAM_REQUEST);
这是我的onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 2)
part = (Part) data.getSerializableExtra("Part");
Log.e(TAG, "onActivityResult: ");
if(requestCode == 1313)
Bitmap thumbnail = (Bitmap)data.getExtras().get("data");
String id = data.getStringExtra("Id");
Log.e(TAG, "onActivityResult: " + id);
但是,Bitmap thumbnail = (Bitmap)data.getExtras().get("data")
是 null
并且 resultCode 始终为 -1。看起来意图没有达到activity
。知道为什么吗?
【问题讨论】:
关注this它可能会帮助您找到解决方案。// Error occurred while creating the File
.而且你仍然继续,好像什么也没发生一样。你应该展示一个这样说的祝酒词然后返回。
photoFile = createImageFile()
。除了检查目录是否可写外,无需创建该文件。可以直接删除。将创作留给相机应用。
感谢 greenapps ,它有效。但是,我还有一个问题 :) 当我设置这样的附加功能时: Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra("Id",id);只有照片是正确的,id 返回 null。你知道为什么吗?
有什么解决办法吗?
【参考方案1】:
适用于所有版本的完整演示。
对于捕获和选择图像
注意:Android 6.0 在点击后获得运行时权限,然后执行。
public class MainActivity extends Activity
private ImageView camera, gallery;
File photoFile;
Uri uri;
private final static int RESULT_LOAD_IMAGE = 1;
private final static int REQUEST_IMAGE_CAPTURE = 2;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
camera = (ImageView) findViewById(R.id.Img);
gallery = (ImageView) findViewById(R.id.imageView);
camera.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
dispatchTakePictureIntent();
);
gallery.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
);
private void dispatchTakePictureIntent()
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
photoFile = Environment.getExternalStoragePublicDirectory("/myimage/save.jpg");
if (photoFile != null)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
uri = data.getData();
String selecteadImage = getRealPathFromURI(this, data.getData());
Toast.makeText(this, "Image path " + selecteadImage, Toast.LENGTH_LONG).show();
else if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
if (data != null && data.getData() != null)
String selecteadImage = getRealPathFromURI(this, data.getData());
Toast.makeText(this, "Image path " + selecteadImage, Toast.LENGTH_LONG).show();
else
if (photoFile != null)
String selecteadImage = photoFile.getAbsolutePath();
Toast.makeText(this, "Image path " + selecteadImage, Toast.LENGTH_LONG).show();
public String getRealPathFromURI(Context context, Uri contentUri)
Cursor cursor = null;
try
String[] proj = MediaStore.Images.Media.DATA;
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
finally
if (cursor != null)
cursor.close();
清单权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
android:background="@drawable/image_1">
<ImageView
android:id="@+id/camera"
android:layout_
android:layout_
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@mipmap/ic_launcher" />
<ImageView
android:id="@+id/gallery"
android:layout_
android:layout_
android:layout_below="@+id/camera"
android:layout_centerHorizontal="true"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
【讨论】:
以上是关于startActivityForResult 返回数据 null的主要内容,如果未能解决你的问题,请参考以下文章
如何从 TabHost Activity 返回结果(startActivityForResult)?
如何从 TabHost Activity 返回结果(startActivityForResult)?
startActivityForResult 返回错误的活动
在 startActivityForResult 返回后,Android parentActivity 没有重新创建