我想将我的设备相机拍摄的图像设置为图像视图,当我这样做时,图像正在旋转。所以我尝试了这段代码,但它抛出了NullPointer异常。我无法理解这个问题,任何人都可以帮忙吗?
java.lang.NullPointerException
at android.content.ContentResolver.openInputStream(ContentResolver.java:490)
at project1.me.com.kupdate.ImageUploadActivity.getCorrectlyOrientedImage(ImageUploadActivity.java:270)
at project1.me.com.kupdate.ImageUploadActivity.onCreate(ImageUploadActivity.java:136)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
我的java代码是
public static int getOrientation(Context context, Uri photoUri) {
/* it's on the external media. */
Cursor cursor = context.getContentResolver().query(photoUri,
new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
if (cursor.getCount() != 1) {
return -1;
}
cursor.moveToFirst();
return cursor.getInt(0);
}
public static Bitmap getCorrectlyOrientedImage(Context context, Uri photoUri) throws IOException {
InputStream is = context.getContentResolver().openInputStream(photoUri);
Log.e("Bitmap", "Bitmap ok ");
BitmapFactory.Options dbo = new BitmapFactory.Options();
dbo.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, dbo);
is.close();
int rotatedWidth, rotatedHeight;
int orientation = getOrientation(context, photoUri);
if (orientation == 90 || orientation == 270) {
rotatedWidth = dbo.outHeight;
rotatedHeight = dbo.outWidth;
} else {
rotatedWidth = dbo.outWidth;
rotatedHeight = dbo.outHeight;
}
Bitmap srcBitmap;
is = context.getContentResolver().openInputStream(photoUri);
if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {
float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
float maxRatio = Math.max(widthRatio, heightRatio);
// Create the bitmap from file
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = (int) maxRatio;
srcBitmap = BitmapFactory.decodeStream(is, null, options);
} else {
srcBitmap = BitmapFactory.decodeStream(is);
}
is.close();
/*
* if the orientation is not 0 (or -1, which means we don't know), we
* have to do a rotation.
*/
if (orientation > 0) {
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),
srcBitmap.getHeight(), matrix, true);
}
Log.e("Bitmap", "Bitmap ok ");
return srcBitmap;
}
///Camera Image
try {
if (picturePath == null) {
Intent intent3 = getIntent();
state = intent3.getExtras().getInt("state_one");
Log.e(TAG, "Camera Image ");
} if(state==1) {
Intent intent3 = getIntent();
Log.e(TAG, "Camera Image Inside ");
picturePath = intent3.getExtras().getString("filePath");
camImageUri = intent3.getParcelableExtra("filePath");
imageView = (ImageView) findViewById(R.id.imgView);
Log.e(TAG, "Before method ");
// decodeFile(picturePath);
try {
bitmap = getCorrectlyOrientedImage(ImageUploadActivity.this, camImageUri);
}
catch (Exception e)
{
e.toString();
// e.printStackTrace();
Log.e(e.getClass().getName(), e.getMessage(), e);
stackTrace = Log.getStackTraceString(e);
Toast.makeText(getApplicationContext(), "stackTrace" +stackTrace, Toast.LENGTH_SHORT).show();
}
Toast.makeText(getApplicationContext(), "stackTrace" +stackTrace, Toast.LENGTH_SHORT).show();
Log.e(TAG, "After method ");
String temp= BitMapToString(bitmap);
Toast.makeText(getApplicationContext(),
"Camera"+temp, Toast.LENGTH_SHORT).show();
if(bitmap==null)
{
Log.e(TAG, "Bitmap is null ");
}
imageView.setImageBitmap(bitmap);
// Toast.makeText(getApplicationContext(), "PATH" + picturePath, Toast.LENGTH_SHORT).show();
bitmap = ShrinkBitmap(picturePath, 300, 300);
}
的OnCreate
Oncreate method-Here I am calling getCorrectlyOrientedImage() method
///Camera Image
try {
if (picturePath == null) {
Intent intent3 = getIntent();
state = intent3.getExtras().getInt("state_one");
Log.e(TAG, "Camera Image ");
} if(state==1) {
Intent intent3 = getIntent();
Log.e(TAG, "Camera Image Inside ");
picturePath = intent3.getExtras().getString("filePath");
camImageUri = intent3.getParcelableExtra("filePath");
imageView = (ImageView) findViewById(R.id.imgView);
Log.e(TAG, "Before method ");
// decodeFile(picturePath);
try {
bitmap = getCorrectlyOrientedImage(ImageUploadActivity.this, camImageUri);
}
catch (Exception e)
{
e.toString();
// e.printStackTrace();
Log.e(e.getClass().getName(), e.getMessage(), e);
stackTrace = Log.getStackTraceString(e);
}
imageView.setImageBitmap(bitmap);
bitmap = ShrinkBitmap(picturePath, 300, 300);
}
答案 0 :(得分:0)
您必须存储" picturePath",在图像捕获过程中,可能会重新创建活动,以便您获得" picturePath"为null。您可以使用onSaveInstanceState和onRestoreInstanceState方法存储图像路径。同时浏览this link