【问题标题】:How do I save an ImageView as an image?如何将 ImageView 保存为图像?
【发布时间】:2012-01-05 22:47:38
【问题描述】:

我有一个带有共享意图的 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"));

}   
}

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

【问题讨论】:

  • 应该单独提出一个新问题,因为您不能为一个问题授予多个答案。但无论如何,File 有一个 mkdirs() 方法,可以用来确保指定的文件夹存在。
  • 哦,好的,谢谢@AlbeyAmakiir!注意到以后的帖子!

标签: android save imageview photo


【解决方案1】:

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

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

【讨论】:

  • 我已经添加了,但它仍然无法正常工作..有什么建议吗?
【解决方案2】:

我遇到了几个没有解决这个问题的解决方案。

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

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

/* 第 1 步 - 使用下面的文件保存功能在本地保存位图文件。 */

localAbsoluteFilePath = saveImageLocally(bitmapImage);

/* STEP 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");
        }


    }   

/* 实用程序 */

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;
    }   
    //...
}

我希望对某人有所帮助。

【讨论】:

  • 我在第 2 步出错了(什么是 utils?):文件 outputDir = Utils.getAlbumStorageDir(Environment.DIRECTORY_DOWNLOADS);
  • @DinIslamMilon,对此感到抱歉,这已经有一段时间了。我添加了我使用的自定义 utils 静态类。它获取用户图片目录
猜你喜欢
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多