简体   繁体   中英

How to retrieve & share bitmap from device folder

I am building a meme generator app. So far, the app does everything I need it to do but I want to add 1 more functionality. The app saves the meme as a bitmap on my device. I want to add a functionality where, if the user wishes to share the meme on Facebook, Twitter, IG, etc, the app retrieves that same bitmap (which was just created.).

I'm not worried about the sharing function at the moment. I want to find out how I can retrieve the file to be able to share it.

This is the method where the creation and saving of the meme takes place. I will omit unnecessary code:

public void createBitmapAndSave(ImageView img) {

        ...

        counter++;

        File file;
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath();
        file = new File(path + "/SimpliMeme/" + timeStamp + "-" + counter + ".jpg");
        file.getParentFile().mkdir();

        try {
            OutputStream stream = new FileOutputStream(file);
            mutableBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            stream.flush();
            stream.close();
            Toast.makeText(getContext(), "Top Text: " + String.valueOf(topTextMeasurement) + " and bottom text: " + String.valueOf(bottomTextMeasurement),
                    Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Uri contentUri = Uri.fromFile(file);
        mediaScanIntent.setData(contentUri);
        Objects.requireNonNull(getContext()).sendBroadcast(mediaScanIntent);
    }

and I've created an empty method that needs to be called when the user hits the "Share" button:

public void shareMeme(){}

Like you've mentioned earlier, you gotta divide this big method into 2 small ones.

As far as your initial question goes: There is no point in going back to your device just to find and get that same photo. Divide the method into 2 methods ( createBitmap() & saveBitmap() , for example) and if the user just wants to share it without saving it to the device, the app will just call the createBitmap() method to only create the bitmap and then you'll use that in your upcoming sharing functionality.

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