How do I save an ImageView as an image?

前端 未结 2 1220
一生所求
一生所求 2020-12-19 01:55

I have an ImageView with a share intent( which works great, brings up all the apps I can share the image with), however, I can not share the photo because it has no path on

相关标签:
2条回答
  • 2020-12-19 02:26

    When saving and loading, you need to get the root path of the system, first. This is how I'd do it.

    File root = Environment.getExternalStorageDirectory();
    File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
    
    0 讨论(0)
  • 2020-12-19 02:46

    I've come across a couple solutions which are not solving this problem.

    Here is a solution that worked for me. One gotcha is you need to store the images in a shared or non app private location (http://developer.android.com/guide/topics/data/data-storage.html#InternalCache)

    Many suggestions say to store in the Apps "private" cache location but this of course is not accessable via other external applications, including the generic Share File intent which is being utilised. When you try this, it will run but for example dropbox will tell you the file is no longer available.

    /* STEP 1 - Save bitmap file locally using file save function below. */

    localAbsoluteFilePath = saveImageLocally(bitmapImage);
    

    /* STEP 2 - Share the non private Absolute file path to the share file intent */

    if (localAbsoluteFilePath!=null && localAbsoluteFilePath!="") {
    
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        Uri phototUri = Uri.parse(localAbsoluteFilePath);
    
        File file = new File(phototUri.getPath());
    
        Log.d("file path: " +file.getPath(), TAG);
    
        if(file.exists()) {
            // file create success
    
        } else {
            // file create fail
        }
        shareIntent.setData(phototUri);
        shareIntent.setType("image/png");
        shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
        activity.startActivityForResult(Intent.createChooser(shareIntent, "Share Via"), Navigator.REQUEST_SHARE_ACTION);
    }   
    

    /* SAVE IMAGE FUNCTION */

        private String saveImageLocally(Bitmap _bitmap) {
    
            File outputDir = Utils.getAlbumStorageDir(Environment.DIRECTORY_DOWNLOADS);
            File outputFile = null;
            try {
                outputFile = File.createTempFile("tmp", ".png", outputDir);
            } catch (IOException e1) {
                // handle exception
            }
    
            try {
                FileOutputStream out = new FileOutputStream(outputFile);
                _bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.close();
    
            } catch (Exception e) {
                // handle exception
            }
    
            return outputFile.getAbsolutePath();
        }
    

    /* STEP 3 - Handle Share File Intent result. Need to remote temporary file etc. */

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
                // deal with this with whatever constant you use. i have a navigator object to handle my navigation so it also holds all mys constants for intents
            if (requestCode== Navigator.REQUEST_SHARE_ACTION) {
                // delete temp file
                File file = new File (localAbsoluteFilePath);
                file.delete();
    
                Toaster toast = new Toaster(activity);
                toast.popBurntToast("Successfully shared");
            }
    
    
        }   
    

    /* UTILS */

    public class Utils {
        //...
        public static File getAlbumStorageDir(String albumName) {
    
            // Get the directory for the user's public pictures directory.
            File file = 
                new File(Environment.getExternalStorageDirectory(), albumName);
            if (!file.mkdirs()) {
                Log.e(TAG, "Directory not created");
            }
            return file;
        }   
        //...
    }
    

    I hope that helps someone.

    0 讨论(0)
提交回复
热议问题