试图共享的图像 [英] Trying to share the image

查看:78
本文介绍了试图共享的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有图像阵列,它们被显示在图像视图。我想分享我想从其中任何图像。我已经写了code,但它也有一些问题。出现的弹出窗口,但图像不能共享,它说无法加载图像。

下面是我的code:

 私人无效shareImage(){
        意图sharingIntent =新的意图(Intent.ACTION_SEND);
        乌里screenshotUri = Uri.parse((拇指[J]));
        尝试 {
            InputStream的流= getContentResolver()。openInputStream(
                    screenshotUri);
        }赶上(FileNotFoundException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
        sharingIntent.setType(为image / jpeg);
        sharingIntent.putExtra(Intent.EXTRA_STREAM,screenshotUri);
        startActivity(Intent.createChooser(sharingIntent,分享图像使用));

    }
 

和我初始化这个 shareImage()在我onClickListener。

拇指是我的字符串数组。

我究竟做错了什么?

修改

 私有String []拇指=新的String [] {

            http://i1040.photobucket.com/albums/b406/Aleem_Ahmed/seven-min_zpsdnohn4io.png
            http://i1040.photobucket.com/albums/b406/Aleem_Ahmed/one-min_zpsxdzul6kt.png};
 

解决方案

您不能直接共享远程图像。份额之前的图像需要下载它在该装置的本地存储。为了下载图片最简单的方法是的AsyncTask

这是一个快速验证的概念,你可以看到图像被下载到缓存目录,之后分享:

 公共类SampleActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_sample);

        findViewById(android.R.id.button1).setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图查看){
                新ShareImageTask()执行(http://s.imgur.com/images/logo-1200-630.jpg);
            }
        });
    }

    私有类ShareImageTask扩展的AsyncTask<字符串,太虚,字符串> {

        @覆盖
        保护字符串doInBackground(字符串...网址){
            字符串URL =网址[0];
            字符串路径= NULL;

            尝试 {
                的InputStream的InputStream =新的java.net.URL(URL).openStream();
                点阵位图= BitmapFactory.de codeStream(InputStream的);

                文件outputDir = getExternalCacheDir(); //上下文作为活动的指针
                文件OUTPUTFILE = File.createTempFile(share_image,巴纽,outputDir);
                FileOutputStream中FOS =新的FileOutputStream(OUTPUTFILE);

                bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,FOS);
                fos.close();

                PATH = outputFile.getPath();
            }赶上(例外五){
                e.printStackTrace();
                PATH = NULL;
            }

            返回路径;
        }

        保护无效onPostExecute(字符串结果){
            如果(TextUtils.isEmpty(结果)){
                Toast.makeText(SampleActivity.this,发生错误,Toast.LENGTH_LONG).show();
                返回;
            }

            份额(结果);
        }
    }

    私人无效份额(字符串路径){
        档案文件=新的文件(路径);

        意图sharingIntent =新的意图(Intent.ACTION_SEND);
        sharingIntent.setType(图像/ *);
        sharingIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(文件));
        startActivity(Intent.createChooser(sharingIntent,分享图像使用));
    }
}
 

不要忘了添加这两个权限你的的Andr​​oidManifest.xml

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
<使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
 

例如在code,你可以改变你的 shareImage 的方法如下:

 私人无效shareImage(){
    新ShareImageTask()执行(拇指[J]);
}
 

I have an array of images, they are being displayed in an image view. I want to share any image i want out of them. I have written a code but it has some problem. The popup window appears but the image cannot be shared, it says failed to load image.

Here is my code:

private void shareImage() {
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        Uri screenshotUri = Uri.parse((thumb[j]));
        try {
            InputStream stream = getContentResolver().openInputStream(
                    screenshotUri);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        sharingIntent.setType("image/jpeg");
        sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
        startActivity(Intent.createChooser(sharingIntent, "Share image using"));

    }

And i am initializing this shareImage() in my onClickListener.

thumb is my string array.

What am i doing wrong?

EDIT

private String[] thumb = new String[] {

            "http://i1040.photobucket.com/albums/b406/Aleem_Ahmed/seven-min_zpsdnohn4io.png",
            "http://i1040.photobucket.com/albums/b406/Aleem_Ahmed/one-min_zpsxdzul6kt.png" };

解决方案

You can't directly share a remote image. Before share the image you need to download it in the local storage of the device. To download the image the simplest method is the AsyncTask.

This is a fast proof-of-concept, as you can see the image is downloaded in the cache directory and after that shared:

public class SampleActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample);

        findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new ShareImageTask().execute("http://s.imgur.com/images/logo-1200-630.jpg");
            }
        });
    }

    private class ShareImageTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {
            String url = urls[0];
            String path = null;

            try {
                InputStream inputStream = new java.net.URL(url).openStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                File outputDir = getExternalCacheDir(); // context being the Activity pointer
                File outputFile = File.createTempFile("share_image", ".png", outputDir);
                FileOutputStream fos = new FileOutputStream(outputFile);

                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.close();

                path = outputFile.getPath();
            } catch (Exception e) {
                e.printStackTrace();
                path = null;
            }

            return path;
        }

        protected void onPostExecute(String result) {
            if (TextUtils.isEmpty(result)) {
                Toast.makeText(SampleActivity.this, "Error occurred", Toast.LENGTH_LONG).show();
                return;
            }

            share(result);
        }
    }

    private void share(String path) {
        File file = new File(path);

        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("image/*");
        sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        startActivity(Intent.createChooser(sharingIntent, "Share image using"));
    }
}

Don't forget to add these two permission to your AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

For example in your code you can change your shareImage method to this:

private void shareImage() {
    new ShareImageTask().execute(thumb[j]);
}

这篇关于试图共享的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆