【问题标题】:how to send drawable image resource using setResult() method from called activity to calling activity如何使用 setResult() 方法将可绘制图像资源从被调用的活动发送到调用活动
【发布时间】:2013-01-18 16:48:02
【问题描述】:

大家好,我是一个开发安卓应用程序的新手,我有一个应用程序说whatsapp,我们可以使用它从图库中选择图片并将其发送给我们的朋友。同样,我正在创建一个类似的应用程序的活动来响应 ACTION_PICK 意图..

假设我已经使用 getIntent() 从 whatsapp 接收到我的活动的意图,并且我将使用 setResult() 将结果发回。在这之间我想知道如何从我的应用中插入可绘制图像资源通过 setResult 到 whats 应用程序,以便 whatsapp 可以接受我在我的应用程序中单击的图像并将其发送给我的朋友。

以下代码是来自 developer.android.com 的参考

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    // Get the intent that started this activity
    Intent intent = getIntent();
    Uri data = intent.getData();

    // Figure out what to do based on the intent type
    if (intent.getType().indexOf("image/") != -1) {
        // Handle intents with image data ...

       **// wanted to know what code must be entered here.** 


    } else if (intent.getType().equals("text/plain")) {
        // Handle intents with text ...
    }
}


// Create intent to deliver some kind of result data
Intent result = new Intent("com.example.RESULT_ACTION", Uri.parse("content://result_uri");
setResult(Activity.RESULT_OK, result);
finish();

【问题讨论】:

    标签: android android-intent android-activity


    【解决方案1】:

    传递图像可能会很慢,因为有时图像可能非常大。您可以将其保存到硬盘并将文件路径传回您的活动并让活动打开它。

    【讨论】:

      【解决方案2】:

      最简单的方法是@wtsang02 建议的。如果您不想接触文件系统,还有其他几种方法。

      1. 使用MemoryFile。然后就可以得到一个容易传入Intent的文件描述符(检查thisthis)。

      2. 我认为这是一种更简洁的方法,但您需要对其进行基准测试以查看它是否适合您。您可以获取byte array from your Drawable,将其作为Parcelable 发送,然后从byte[] 重构Drawable:

        byte[] drawableBytes = [your byte array];
        ByteArrayInputStream is = new ByteArrayInputStream(drawableBytes);
        Drawable drw = Drawable.createFromStream(is, "drawable");
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多