我正在尝试从ImageView获取图像的文件路径。我已经尝试了很多使用我在StackOverFlow上看到的实现,但每个都失败了。我正在使用的当前实现有什么问题?我在Android Studio中使用Android 7.0。我的代码中断了:
String pathToImage = pathURI.getPath();
这是我的全部代码:
public class MainActivity extends AppCompatActivity {
static final int REQUEST_IMAGE_CAPUTRE = 1;
ImageView mainImage;
private Uri mImageCaptureUri;
private static final String TAG = "JoelMessage";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button cameraBtn = (Button)findViewById(R.id.cameraBtn);
mainImage = (ImageView) findViewById(R.id.mainImage);
//Disable the button if user has no camera
if(!hasCamera())
cameraBtn.setEnabled(false);
}
//Check if use has a camera
private boolean hasCamera(){
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
//Launching the camera
public void launchCamera(View view){
//intent to launch camera
Log.i(TAG, "camera launched");
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
//Take picture and pass results along to onActivity Result
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPUTRE);
}
//If you want to return the image taken.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// request code makes sure u got an image and resultCode makes sure that u got no errors
if (requestCode == REQUEST_IMAGE_CAPUTRE && resultCode== RESULT_OK ){
Bundle extras = data.getExtras();
Bitmap photo = (Bitmap) extras.get("data"); //get photo info and storing it in photo
ImageView image = (ImageView) findViewById(R.id.mainImage);
image.setImageBitmap(photo);
Log.i(TAG, "set image");
//Log.i(TAG, Integer.toString(x));
//String pathToImage = mImageCaptureUri.getPath();
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri pathURI = getImageUri(getApplicationContext(), photo);
String pathToImage = pathURI.getPath();
//System.out.println(mImageCaptureUri);
photo = imageOreintationValidator(photo, pathToImage);
mainImage.setImageBitmap(photo);
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
}
编辑 我对Android开发很陌生,所以我很难解决哪部分错误消息很重要但是我认为这些错误很重要:
05-10 01:15:56.411 17924-17924/? E/AndroidRuntime: FATAL EXCEPTION: main
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference
at com.example.webbj.cameratest.MainActivity.onActivityResult(MainActivity.java:73)
如果您需要更多错误消息,请随时发表评论。