vendredi 17 juin 2016

Image Scale error in Android

I am creating and application that takes a photo and scale it in order to be displayed and uploaded but it crashes showing me: Failure delivering result ResultInfo.

Here's the code:

static final int REQUEST_TAKE_PHOTO = 1;

private void dispatchTakePictureIntent() {
    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) {
            Log.w("IO exception", ex.toString());
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

-

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        Log.e("Log mCurrentPhotoPath", mCurrentPhotoPath + "...");
        //img.setImageURI(Uri.parse(mCurrentPhotoPath));
        selectedImagePath1 = mCurrentPhotoPath.substring(5);
        Log.e("Log Optimized path", selectedImagePath1 + "...");

        //img.setImageURI(Uri.parse(selectedImagePath1));

        setPic();
        //Bitmap resized = Bitmap.createScaledBitmap(Uri.parse(selectedImagePath1), 200, 200, true);
        Glide.with(this).load(resized).into(img);
        //Bundle extras = data.getExtras();
        //Bitmap imageBitmap = (Bitmap) extras.get("data");

        //img.setImageBitmap(imageBitmap);
    }
}

-

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

-

private void setPic() {
    // Get the dimensions of the View
    int targetW = 500;
    int targetH = 500;

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(selectedImagePath1, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    resized = BitmapFactory.decodeFile(selectedImagePath1, bmOptions);
}

Can you help me, please?

EDIT Here's the log:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.app.photonew/com.app.photonew.MainActivity}: java.lang.IllegalArgumentException: Unknown type class android.graphics.Bitmap. You must provide a Model of a type for which there is a registered ModelLoader, if you are using a custom model, you must first call Glide#register with a ModelLoaderFactory for your custom model class
at android.app.ActivityThread.deliverResults(ActivityThread.java:3389)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3432)
at android.app.ActivityThread.access$1300(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1253)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)

Caused by: java.lang.IllegalArgumentException: Unknown type class android.graphics.Bitmap. You must provide a Model of a type for which there is a registered ModelLoader, if you are using a custom model, you must first call Glide#register with a ModelLoaderFactory for your custom model class at com.bumptech.glide.RequestManager.loadGeneric(RequestManager.java:629) at com.bumptech.glide.RequestManager.load(RequestManager.java:598) at it.carloveronesi.photonew.MainActivity.onActivityResult(MainActivity.java:76) at android.app.Activity.dispatchActivityResult(Activity.java:5423) at android.app.ActivityThread.deliverResults(ActivityThread.java:3385) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3432)  at android.app.ActivityThread.access$1300(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1253)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5146)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)  at dalvik.system.NativeStart.main(Native Method) 

Aucun commentaire:

Enregistrer un commentaire