在另一个应用程序中打开图像。

4

这是我的应用程序的外观:

enter image description here

这是我想要在按钮点击时执行的操作:在另一个应用程序中打开图像

enter image description here

这是我尝试过的:

package com.example.wallpaper_test;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class WallpaperScreenActivity extends ActionBarActivity {

    public static final int REQUEST_CODE = 1;

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

        // image resource
        ImageView img = (ImageView) findViewById(R.id.imageView1);
        img.setImageResource(R.drawable.pop);

        // call installed wallpaper app to set wallpaper on button click
        Button b1 = (Button) findViewById(R.id.button1);
        b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View vx) {


                Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
                intent.addCategory(Intent.CATEGORY_DEFAULT);                
                intent.setDataAndType(uri.setreso, "image/jpeg");
                intent.putExtra("mimeType", "image/jpeg");
                this.startActivity(Intent.createChooser(intent, "Set as:"));

            }

        });

    }

}
我遇到的这些错误:
Description Resource    Path    Location    Type

uri cannot be resolved to a variable    WallpaperScreenActivity.java    /Wallpaper_Test/src/com/example/wallpaper_test  line 32 Java Problem

The method startActivity(Intent) is undefined for the type new View.OnClickListener(){} WallpaperScreenActivity.java    /Wallpaper_Test/src/com/example/wallpaper_test  line 34 Java Problem

我创建了一个非常简单的应用程序,其中我在image_view中显示一张图片,并希望在按下按钮时在另一个应用程序中打开此图片。


只需从this.startActivity()中删除this,直接使用startActivity()即可。 - joao2fast4u
你的代码没问题,你只需要向Intent传递一个有效的Uri。你想要传递什么样的Uri? - joao2fast4u
实际上我想传递图片,这样我就可以用另一个应用程序打开它。 - user3739970
我该如何将图像视图图像传递给 URI? - user3739970
你解决了你的问题吗? - joao2fast4u
我尝试了这个代码:Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse("android.resource://com.example.wallpaper_test/drawable/pop"),"image/*"); startActivity(intent);但是遗憾的是,应用程序停止运行了。 - user3739970
2个回答

3
尝试使用以下代码:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + "/sdcard/test.jpg"), "image/*");
startActivity(intent);

如何将ImageView的图像传递给URI? - user3739970
您不能传递ImageView。如果要在您自己的应用中显示图像,则可以使用ImageView将图像显示在您自己的应用程序中。 - Umer Kiani
好的,现在我明白了,是的,有一种方法。我会编辑问题。 - Umer Kiani
根据你的代码,我尝试了这个intent.setDataAndType(Uri.parse("android.resource://package com.example.wallpaper_test;/drawable/pop"), "image/*");,但是遇到了“不幸停止”的错误。 - user3739970
让我们在聊天中继续这个讨论。 (http://chat.stackoverflow.com/rooms/58092/discussion-between-umer-kiani-and-user3739970) - Umer Kiani
显示剩余2条评论

2

首先我将图像保存到SD卡,然后使用以下代码打开它:

// open image in another app
                    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
                    intent.addCategory(Intent.CATEGORY_DEFAULT);
                    intent.setDataAndType(Uri.parse("file://mnt/sdcard/temp_image0.png"),"image/*");
                    intent.putExtra("mimeType", "image/*");
                    startActivity(Intent.createChooser(intent, "Set as:"));
                    //

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接