【问题标题】:Share a temporary image分享一张临时图片
【发布时间】:2018-07-01 22:17:02
【问题描述】:

我编写了这段代码,让我可以创建一个图像表单 url 并保存为文件以进行共享。如何改进此代码以创建临时图像文件,在共享后或关闭应用程序时删除自己?

public void disegnaBitmap(){
        Glide
            .with(GlideImgActivity.this)
            .load(MY_URL)
            .asBitmap()
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .into(getTarget());

}

private SimpleTarget<Bitmap> getTarget(){
        SimpleTarget<Bitmap> target = new SimpleTarget<Bitmap>(100,100) {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                Log.d(LOG_TAG,"Bitmap caricata");
                shareImgTemp(resource);
            }
            public void onLoadFailed(Exception e, Drawable errorDrawable){
                Log.d(LOG_TAG,"Errore di caricamento 1: "+ e.getMessage());
            }
        };
        return target;
 }

public void shareImg(Bitmap icon){

    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    try {
        bytes.close();
    } catch (IOException e) {
        e.printStackTrace();
    }


    String pathname = Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg";

    File f = new File(pathname);
    try {

        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);

        fo.write(bytes.toByteArray());
        fo.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
    startActivity(Intent.createChooser(share, "Share Image"));
}

我知道可以用

File.createTempFile("myfile", ".jpg");

但是它将文件保存在缓存目录中,然后我不知道如何共享它。 我该怎么办?

【问题讨论】:

    标签: android share android-image android-bitmap temporary-files


    【解决方案1】:
            public void shareImg(Bitmap icon){
    
                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("image/jpeg");
    
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                try {
                    bytes.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
    
                String pathname = Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg";
    
                File f = new File(pathname);
                try {
    
                    f.createNewFile();
                    FileOutputStream fo = new FileOutputStream(f);
    
                    fo.write(bytes.toByteArray());
                    fo.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
    
                        try {
    share.putExtra(Intent.EXTRA_STREAM,FileProvider.getUriForFile(
                                    MainActivity.this,
                                   "Your PackageName",
                                    f));
                startActivity(Intent.createChooser(share, "Share Image"));
    
                        } catch (IllegalArgumentException e) {
                            Log.e("File Selector",
                                  "The selected file can't be shared.");
                        }
    
    
            }
    

    【讨论】:

    • 它需要清单中的提供程序,并且不适用于 android 4.4。此解决方案不允许我创建临时文件。
    猜你喜欢
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多