Android 4.4.2 Kamera - uri null zurück und auch die Beschnitt-Funktion nicht funktioniert?
Ich versuche, klicken Sie auf das Bild von der Kamera und zeigen Sie in die Bildansicht, aber onactivityresult-URI null zurück. Aber unten code läuft wunderbar auf JellyBean , ICS mit zuschneiden-Funktion .
Aufrufen Absicht
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
pickIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//we will handle the returned data in onActivityResult
String pickTitle = "Select or take a new Picture";
//startActivityForResult(captureIntent, 1);
Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]
{ captureIntent });
startActivityForResult(chooserIntent, GlobalVariables.GALLERY_MODE);
onActivityResult
@SuppressLint("NewApi")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
//TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
try
{
if (resultCode == RESULT_OK)
{
//user is returning from capturing an image using the camera
if (requestCode == GlobalVariables.GALLERY_MODE)
{
//get the Uri for the captured image
picUri = data.getData();
System.out.println("picUri ======= => "+picUri);
boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if(isKitKat)
{
System.out.println(new GraphicsUtil().getPath(RegistrationActivity.this, picUri));
String filePath = new GraphicsUtil().getPath(RegistrationActivity.this, picUri);
System.out.println("filepath => "+filePath);
bitmapPreview = null;
bitmapPreview = BitmapFactory.decodeFile(filePath);
System.out.println("bitmapPreview => "+bitmapPreview );
imageViewUser.setImageBitmap(new GraphicsUtil().getCircleBitmap(GraphicsUtil.decodeSampledBitmapFromResource(filePath,
120, 120)));
}
else
{
performCrop(picUri, PIC_CROP, RegistrationActivity.this);
}
}
//user is returning from cropping the image
else if (requestCode == PIC_CROP)
{
String filePath = new File (Environment.getExternalStorageDirectory() + "/temporary_holder.jpg").getAbsolutePath();
bitmapPreview = null;
bitmapPreview = BitmapFactory.decodeFile(filePath);
imageViewUser.setImageBitmap(new GraphicsUtil().getCircleBitmap(GraphicsUtil.decodeSampledBitmapFromResource(filePath,
120, 120)));
//imageViewUser.setImageBitmap(new
//GraphicsUtil().getCircleBitmap(bitmapPreview));
File f = new File(Environment.getExternalStorageDirectory() + "/temporary_holder.jpg");
if (f.exists())
{
f.delete();
}
}
}
}
catch (Exception e)
{
//TODO: handle exception
e.printStackTrace();
}
}
GraphicsUtil.java
public class GraphicsUtil
{
/*
* Draw image in circular shape Note: change the pixel size if you want
* image small or large
*/
public Bitmap getCircleBitmap(Bitmap bitmap)
{
Bitmap output;
Canvas canvas = null;
final int color = 0xffff0000;
final Paint paint = new Paint();
Rect rect = null;
if (bitmap.getHeight() > 501)
{
output = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
canvas = new Canvas(output);
rect = new Rect(0, 0, 500, 500);
}
else
{
//System.out.println("output else =======");
bitmap = Bitmap.createScaledBitmap(bitmap, 500, 500, false);
output = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
canvas = new Canvas(output);
rect = new Rect(0, 0, 500, 500);
}
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setFilterBitmap(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth((float) 1);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
//Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth)
{
//Calculate ratios of height and width to requested height and
//width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
//Choose the smallest ratio as inSampleSize value, this will
//guarantee
//a final image with both dimensions larger than or equal to the
//requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(String path, int reqWidth, int reqHeight)
{
//First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
//Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
//Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
@SuppressLint("NewApi")
public String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
//DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
//ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
//TODO handle non-primary volumes
}
//DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
//MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
//MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
//Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
//File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
}
logcat-Ausgabe beim Klick auf Bild und wählen Sie Bild auf android 4.4.2 os:-
05-08 17:25:55.697: I/System.out(20894): picUri ======= => null
05-08 17:25:55.698: W/System.err(20894): java.lang.NullPointerException
05-08 17:25:55.711: W/System.err(20894): at android.provider.DocumentsContract.isDocumentUri(DocumentsContract.java:587)
05-08 17:25:55.712: W/System.err(20894): at com.asiaelites.utils.GraphicsUtil.getPath(GraphicsUtil.java:119)
05-08 17:25:55.712: W/System.err(20894): at com.asiaelites.RegistrationActivity.onActivityResult(RegistrationActivity.java:489)
05-08 17:25:55.712: W/System.err(20894): at android.app.Activity.dispatchActivityResult(Activity.java:5446)
05-08 17:25:55.712: W/System.err(20894): at android.app.ActivityThread.deliverResults(ActivityThread.java:3442)
05-08 17:25:55.712: W/System.err(20894): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2802)
05-08 17:25:55.712: W/System.err(20894): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2859)
05-08 17:25:55.712: W/System.err(20894): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2274)
05-08 17:25:55.712: W/System.err(20894): at android.app.ActivityThread.access$800(ActivityThread.java:139)
05-08 17:25:55.712: W/System.err(20894): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
05-08 17:25:55.712: W/System.err(20894): at android.os.Handler.dispatchMessage(Handler.java:102)
05-08 17:25:55.712: W/System.err(20894): at android.os.Looper.loop(Looper.java:136)
05-08 17:25:55.712: W/System.err(20894): at android.app.ActivityThread.main(ActivityThread.java:5102)
05-08 17:25:55.712: W/System.err(20894): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 17:25:55.713: W/System.err(20894): at java.lang.reflect.Method.invoke(Method.java:515)
05-08 17:25:55.713: W/System.err(20894): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-08 17:25:55.713: W/System.err(20894): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-08 17:25:55.713: W/System.err(20894): at dalvik.system.NativeStart.main(Native Method)
Folgen Sie den link unten :-
Android-Galerie auf KitKat gibt unterschiedliche Uri für Vorsatz.ACTION_GET_CONTENT
- Es ist ein bekannter bug in der kitkat-wenn ich mich nicht Irre.
- Check meine Antwort hier stackoverflow.com/questions/22576049/...
- versuchen Sie, meine Lösung, wenn Sie immer noch problem
- wenden Sie sich mit diesem link stackoverflow.com/questions/23261498/...
- wenden Sie sich mit diesem link stackoverflow.com/questions/23261498/android-kitkat-image-crop
- Absicht pickPhoto = new Intent( Intent.ACTION_PICK, android.Anbieter.MediaStore.Bilder.Media.EXTERNAL_CONTENT_URI); startActivityForResult(pickPhoto, 2); if ("resultCode" = = getActivity().RESULT_OK) { Uri selectedImage = imageReturnedIntent.getData(); try { // userProfilePic.setImageBitmap(resizedBitmap); performCrop(selectedImage); } catch (Exception e) { e.printStackTrace(); } }
- Dieser arbeitet mit der Galerie, aber nicht die Arbeit mit der Kamera... ich brauche für die Kamera.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Diese Arbeit für
Kitkat
Ich hoffe, du hast die Lösung, aber dies ist für diejenigen, die noch zu finden für die Lösung.
verwenden Sie diese Codezeile in onActivityResult
ODER Beziehen sich diese link für die Anreise URI.
Einfach diesen code verwenden, der getPath(final Context context, final Uri uri), Mit uri, Sie haben es return path der Datei.