Android 将图像返回到whatsapp

Android 将图像返回到whatsapp,android,image,android-intent,return-value,whatsapp,Android,Image,Android Intent,Return Value,Whatsapp,我一直在尝试构建一个应用程序,当用户试图使用whatsapp共享图像时,它会显示为可选的图像源。到目前为止,我已经设法让我的应用程序显示在whatsapp使用意向过滤器启动的服务选择器中,但我无法让图像正确返回到whatsapp。我在下面发布我的代码: public void returnImage(View v){ //Bitmap img; //Bundle selectedImage = new Bundle(); Uri imageURI; Intent

我一直在尝试构建一个应用程序,当用户试图使用whatsapp共享图像时,它会显示为可选的图像源。到目前为止,我已经设法让我的应用程序显示在whatsapp使用意向过滤器启动的服务选择器中,但我无法让图像正确返回到whatsapp。我在下面发布我的代码:

public void returnImage(View v){
    //Bitmap img;
    //Bundle selectedImage = new Bundle();
    Uri imageURI;
    Intent shareIntent = new Intent();
    switch(v.getId()){
    case R.id.eric1 :
        imageURI =  saveToCache(R.drawable.cartman1);
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageURI);
        shareIntent.setType("image/png");
        setResult(RESULT_OK, shareIntent);
        Utils.makeToast("Selected",this);
        System.out.println("--------------------------------");
        System.out.println(imageURI.toString());
        finish();
    }
}

   private Uri saveToCache(int resID) {
    // TODO Auto-generated method stub
    Bitmap image = BitmapFactory.decodeResource(getResources(), resID);
    File imageFile;
    Date d = new Date();
    String imgName = ((Long.toString(d.getTime())).subSequence(1,
            9)).toString();
    String state = Environment.getExternalStorageState();
    printDebug(state);
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        File file = getExternalFilesDir(null);
        if (file != null) {
            try {
                //String root = file.getAbsolutePath();
                imageFile = new File(file, imgName+".png");
                printDebug(imageFile.getAbsolutePath());
                FileOutputStream stream = new FileOutputStream(imageFile);
                boolean complete = image.compress(Bitmap.CompressFormat.PNG, 100, 
                    stream);
                if (!complete) {
                    Log.d("tag", "image not saved");
                }
                Log.d("tag", "image saved");
                // Tell the media scanner about the new file so that it is
                // immediately available to the user.
                MediaScannerConnection.scanFile(this,
                        new String[] { imageFile.toString() }, null,
                        new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        Log.i("ExternalStorage", "Scanned " + path + ":");
                        Log.i("ExternalStorage", "-> uri=" + uri);
                    }
                });

                return Uri.parse(imageFile.getAbsolutePath());
            } catch (IOException e) {
                Log.d("tag", "Can't save image", e);
            }
        }
    }
    return null;
    }
应用程序打开,我选择了图像,但whatsapp报告图像无法共享。LogCat不显示任何错误或警告

我阅读了资源


但是没有提到该应用程序返回的方式或内容,所以我在这里完全不知所措。

在搜索了几天后,这里有一个可行的解决方案,可以将图像返回到所有其他应用程序(经过GMail和WhatsApp测试)

首先,您需要在AndroidManifest.xml(内部应用程序>活动)中设置意图过滤器。当其他应用程序调用此目的时(如请求图像时),此列表将列出您的应用程序。 注意:WhatsApp正在使用action.PICK-intent。在下面添加所有意图过滤器,即使这样也能提供与其他应用程序的良好兼容性

<intent-filter>
                <action android:name="android.intent.action.PICK" /> 
                <category android:name="android.intent.category.DEFAULT"  /> 
                <category android:name="android.intent.category.OPENABLE" />
                <data android:mimeType="image/*" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.OPENABLE" />
                <data android:mimeType="image/*" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.GET_CONTENT" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.OPENABLE" />
                <data android:mimeType="image/*" />
            </intent-filter>
现在,当用户拾取图像后,将结果设置为如下所示。由于内存问题,您应该将要返回的文件复制到SD卡并返回Uri

if(isinint) //check if any app cares for the result
        {
            Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND, Uri.fromFile(openprev)); //Create a new intent. First parameter means that you want to send the file. The second parameter is the URI pointing to a file on the sd card. (openprev has the datatype File)

            ((Activity) context).setResult(Activity.RESULT_OK, shareIntent); //set the file/intent as result
            ((Activity) context).finish(); //close your application and get back to the requesting application like GMail and WhatsApp
            return; //do not execute code below, not important
        }

注意:在OnCreate或类似的void中调用数据时,可以省略
((活动)上下文)
。当我在另一个空白处使用此代码段时,我需要在任何情况下提供一个必须定义为显示的上下文。

Followig适用于我:

    <activity android:name="com.selcuksoydan.sorucevap.Main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
            <data android:mimeType="text/*" />
        </intent-filter>



        Uri imageUri = (Uri) getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
        if (imageUri != null) {

Uri imageUri=(Uri)getIntent().getParcelableExtra(Intent.EXTRA_流);
if(imageUri!=null){

有人可以结束这个问题吗,因为没有人回答。@JamesCameron:有趣的是,我在android市场上看到过这样的应用程序。我不知道他们是否对其进行了反向工程,或者他们是否询问Whatsapp的人员该应用程序期望返回什么。不过,我有一个问题,图像库是什么(默认android gallery)return?这可能与它期望的结果相同,对吧?Hanut,我刚刚解决了这个问题。有关更多信息,请参阅我的答案。嗨,james..我正在尝试使用您的解决方案,但我不知道如何在绘图表中获取图像的Uri。我正在尝试Uri path=Uri.parse(“安卓。resource://com.example.myfragments/drawable/action.png)但这不起作用。当我设置结果()时,它会给我空指针。你能帮忙吗?谷歌会帮你的:@JamesCameron你能根据你的答案回答这个问题吗
    <activity android:name="com.selcuksoydan.sorucevap.Main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
            <data android:mimeType="text/*" />
        </intent-filter>



        Uri imageUri = (Uri) getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
        if (imageUri != null) {