Поделиться временным изображением - PullRequest
0 голосов
/ 02 июля 2018

Я написал этот код, который позволил мне создать 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");

но он сохраняет файл в каталоге кеша, а потом я не знаю, как принять его. Как я могу это сделать?

1 Ответ

0 голосов
/ 02 июля 2018
        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.");
                    }


        }
...