将图像返回到whatsapp

Han*_*nut 4 android image return-value android-intent whatsapp

当用户尝试使用whatsapp共享图像时,我一直在尝试构建一个显示为可选图像源的应用程序.到目前为止,我已经设法让我的应用程序显示在服务选择器中whatsapp使用intent过滤器启动但我无法让图像正确返回到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;
    }
Run Code Online (Sandbox Code Playgroud)

该应用程序打开,我选择图像但whatsapp报告无法共享图像.LogCat不显示错误或警告.

我读了Whatsapp的资源 Intent-Filter - > share image

但没有提到应用程序返回的方式或内容,所以我在这里完全失败了.

Jam*_*ron 11

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

首先,您需要在AndroidManifest.xml(Inside application > activity)中设置intent-filter .当其他应用程序调用此意图时(例如,在请求图像时),这将列出您的应用程序. 注意: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>
Run Code Online (Sandbox Code Playgroud)

你需要关心的第二件事是回应闲置的意图.这应该由两部分组成:首先,您应该检查您的应用程序是否已被执行以返回图像,或者它是否自行运行.

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.
        }
Run Code Online (Sandbox Code Playgroud)

现在,当用户选择图像时,将结果设置如下.由于内存问题,您应该将要返回的文件复制到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
        }
Run Code Online (Sandbox Code Playgroud)

注意!:((Activity) context)在OnCreate或类似的void中调用数据时可以省略.当我在另一个void中使用这个片段时,我需要在任何必须定义为显示的情况下提供上下文.