返回一个图像到WhatsApp的图像、WhatsApp

2023-09-12 10:23:04 作者:少女季节

我一直在试图建立一个应用程序,显示为一个可选的图像源,当用户试图使用WhatsApp的共享图像。到目前为止,我设法让我的应用程序,以显示在服务选取器WhatsApp的使用意图过滤器的推出,但我不能让图像以正确地返回到WhatsApp的。进出口张贴低于我的code:

I've been trying to build an app that shows up as an optional image source when a user tries to share an image using whatsapp. So far I have managed to get my app to show up in the service picker that whatsapp launches using intent filters but I cannot get the image to return correctly to whatsapp. Im posting my code below :

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中显示没有错误或警告。

The app opens and i select the image but whatsapp reports that the image cannot be shared. LogCat shows no errors or warnings.

我读了资源 Intent-Filter对于WhatsApp的 - >分享图片

但没有提及怎样还是怎样由应用程序返回的,所以我在这里完全丧失。

but there is no mention of how or what was returned by the app so I'm at a complete loss here.

推荐答案

搜索天后,这里是一个可行的解决方案,以恢复图像的所有其他应用程序(测试Gmail和WhatsApp的)。

After searching for days, here is a working solution to return images to all other applications (tested for GMail and WhatsApp).

首先,您需要在您的 AndroidManifest.xml中设置一个意图过滤器(里面的应用程序的>的活动的)。这将列出您的应用程序时,其他应用程序都在呼吁这个意图(请求图像时等)。 注意:WhatsApp的是使用action.PICK - 意图。将所有的意图,下面的过滤器,即使将提供极大的兼容性与其他应用程序。

First, you need to set an intent-filter in your AndroidManifest.xml (Inside application > activity). This will list your application when other apps are calling for this intent (like when requesting an image). Note: WhatsApp is using the action.PICK - intent. Adding all intent-filters below even though will provide great compatibility with other apps.

<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>

您需要照顾的响应空转意图的第二件事。 这应该由两部分组成:首先,你应该检查是否您的应用程序已执行回的图像,或者其自身运行的所有

The second thing you need to care for is responding to an idling intent. This should consist of two parts: First you should check whether your application has been executed to return an image or if its run all by itself.

Intent intent = getIntent();
        if (intent!=null && intent.getType()!=null)  //check if any application has executed your app
        {
             if(intent.getType().indexOf("image/") != -1) isinint=true; //check if the requested type is an image. If true, set a public static boolean, f.e. named isinint to true. Default is false.
        }

现在,当用户已经选择了一个图像,设置结果如下。由于内存问题,您应该复制要返回到SD卡的文件,并返回URI。

Now, when the user has picked an image, set the result as following. Due to memory issues, you should copy the file you want to return onto the sdcard and return the 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
        }

WhatsApp去年亏1.3亿美元 投资者不担心

注意:您可以省略((活动)范围内)调用的OnCreate或similiar无效的数据时。因为我用这个片段在另一个空间,我需要提供一个背景在一个拥有显示要定义的任何情况。

Note!: You can leave out ((Activity) context) when calling the data in OnCreate or similiar void's. As i use this snippet in another void, i need to provide a context in any case that has to be defined as displayed.