我总是在 OnActivityResult 方法中获取数据的空值。我尝试使用没有附加功能的意图,但这对我也不起作用。
package com.mypack;
import java.io.ByteArrayOutputStream;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
public class PhotoUploadRecommenderActivity extends Activity {
private Button accettoButton;
private static final int CAM_REQUEST = 1337;
private Uri mCapturedImageURI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_upload_recommendation);
setUpView();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAM_REQUEST && resultCode == RESULT_OK) {
// Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
//imgTakenPhoto.setImageBitmap(thumbnail);
String[] projection = { MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String capturedImageFilePath = cursor.getString(column_index_data);
// your bitmap
ByteArrayOutputStream bs = new ByteArrayOutputStream();
//thumbnail.compress(Bitmap.CompressFormat.PNG, 0, bs);
Intent intent = new Intent(getApplicationContext(),PhotoPreviewActivity.class);
intent.putExtra("byteArray", capturedImageFilePath);
startActivity(intent);
}
}
private void setUpView() {
accettoButton = (Button)findViewById(R.id.accetto_button);
accettoButton.setOnClickListener(new btnTakePhotoClicker());
}
class btnTakePhotoClicker implements Button.OnClickListener
{
public void onClick(View v) {
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(cameraIntent,CAM_REQUEST);
}
}
}
我想使用相机意图捕捉图像。我收到此错误,其中还包括我的光标上的空指针异常。
java.lang.RuntimeException: Unable to resume activity {com.mypack/com.mypack.PhotoUploadRecommenderActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=null} to activity {com.mypack/com.mypack.PhotoUploadRecommenderActivity}: java.lang.NullPointerException