如何将 ImageView 保存为图像?

发布于 2024-12-18 02:11:39 字数 1061 浏览 3 评论 0原文

我有一个具有共享意图的 ImageView(效果很好,可以调出我可以共享图像的所有应用程序),但是,我无法共享照片,因为它在我的手机上没有路径。如何将 ImageView 保存到手机上?下面是我的代码。

 public void taptoshare(View v)
{  
    View content = findViewById(R.id.myimage);
    content.setDrawingCacheEnabled(true);
        Bitmap bitmap = content.getDrawingCache();
        File file = new File("/DCIM/Camera/image.jpg");
        try 
        {
            file.createNewFile();
            FileOutputStream ostream = new FileOutputStream(file);
            bitmap.compress(CompressFormat.JPEG, 100, ostream);
            ostream.close();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }


    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    Uri phototUri = Uri.parse("/DCIM/Camera/image.jpg");
    shareIntent.setData(phototUri);
    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
    startActivity(Intent.createChooser(shareIntent, "Share Via"));

}   
}

更新 好的,所以我想通了。现在我有一个新问题,如何将此图像保存到新文件夹?

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 my phone. How do I go about saving the ImageView on my phone? Below is my code.

 public void taptoshare(View v)
{  
    View content = findViewById(R.id.myimage);
    content.setDrawingCacheEnabled(true);
        Bitmap bitmap = content.getDrawingCache();
        File file = new File("/DCIM/Camera/image.jpg");
        try 
        {
            file.createNewFile();
            FileOutputStream ostream = new FileOutputStream(file);
            bitmap.compress(CompressFormat.JPEG, 100, ostream);
            ostream.close();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }


    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    Uri phototUri = Uri.parse("/DCIM/Camera/image.jpg");
    shareIntent.setData(phototUri);
    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
    startActivity(Intent.createChooser(shareIntent, "Share Via"));

}   
}

UPDATE
Ok, so I figured it out. Now I have a new question, how would I go about saving this image to a new folder?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧情别恋 2024-12-25 02:11:39

保存和加载时,首先需要获取系统的根路径。我就是这样做的。

File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");

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");
多情癖 2024-12-25 02:11:39

我遇到了一些无法解决此问题的解决方案。

这是一个对我有用的解决方案。一个问题是您需要将图像存储在共享或非应用程序私有位置(http://developer.android.com/guide/topics/data/data-storage.html#InternalCache

许多建议都说存储在Apps“私有”缓存中位置,但这当然不是可通过其他外部应用程序访问,包括正在使用的通用共享文件意图。当您尝试此操作时,它将运行,但例如 dropbox 会告诉您该文件不再可用。

/* 步骤 1 - 使用下面的文件保存函数在本地保存位图文件。 */

localAbsoluteFilePath = saveImageLocally(bitmapImage);

/* 步骤 2 - 将非私有绝对文件路径共享到共享文件意图 */

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);
}   

/* 保存图像功能 */

    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();
    }

/* 步骤 3 - 处理共享文件意图结果。需要远程临时文件等。 */

    @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'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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文