简体   繁体   中英

Loading a bitmap with ImageDecoder from URI and adding text

In my activity, I want to modify an image to add a text on it.

The image is selected in the galery or taken with the camera and then stored in a file in a previous activity. Then the uri of that file is passed through extras.

Now I try to add a string on top of the image like so:

try {
        modifyThePic(imageUri);
    } catch (IOException e) {
        e.printStackTrace();
    }

here is the function's body:

public void modifyThePic(Uri imageUri) throws IOException {

    ImageDecoder.Source source = ImageDecoder.createSource(this.getContentResolver(), imageUri);
    Bitmap bitmap = ImageDecoder.decodeBitmap(source);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setTextSize(10);
    canvas.drawText("Some Text here", 0, 0, paint);

    image.setImageBitmap(bitmap); //image is the imageView to control the result

}

The expected behaviour would be to display the image with "Some Text here" on top of it. But instead there is nothing displayed, however the app doesn't crash.

While debugging, I come across an error that appears between the

Bitmap bitmap = ImageDecoder.decodeBitmap(source);

and the

Canvas canvas = new Canvas(bitmap);

here is the error:

java.io.FileNotFoundException: No content provider: /storage/emulated/0/Android/data/com.emergence.pantherapp/files/Pictures/JPEG_20200829_181926_7510981182141824841.jpg

I suspect that I missuse the "ImageDecoder" as it's my first time using it. More precisely, I was not able to let the "decodeBitmap" method in onCreate, Android Studio was telling me that it could not be in the "main thread", I am not familiar at all with threading. Moving it in a dedicaded function fixed this but maybe am I supposed to do something else and it's the root of the problem.

My questions: Am I using the right tools to modify the file and add the text on it ? If yes, what do I do wrong ? If no, what library/tools should I look into to do this task ?

Thank you,

EDIT: ADDITONAL ANSWER ELEMENTS

As both @blackapps and @rmunge pointed out, I was not getting a legit URI but instead a file path. The easy fix for my problem was to get the URI from the path using this code:

Uri realuri = Uri.fromFile(new File("insert path here")));

Additionaly to edit the bitmap, it must be made mutable which is covered here for example.

The final function to extract the bitmap from the URI and adding text on top of it is this one:

public void modifyThePic(Uri imageUri) throws IOException {

    ContentResolver cr = this.getContentResolver();
    InputStream input = cr.openInputStream(imageUri);
    Bitmap bitmap = BitmapFactory.decodeStream(input).copy(Bitmap.Config.ARGB_8888,true);

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setTextSize(300);

    int xPos = (canvas.getWidth() / 2); //just some operations to center the text
    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;

    canvas.drawText("SOME TEXT TO TRY IT OUT", xPos, yPos, paint);

    image.setImageBitmap(bitmap);

}

Seems your URI is wrong. It has to start with file:///

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM