这就是我的应用的外观:
这是我想要点击按钮时执行的操作:在其他应用中打开图片
这是我尝试过的:
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中显示图像,我想在按钮单击时在另一个应用程序中打开此图像。
答案 0 :(得分:3)
尝试使用以下代码..
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + "/sdcard/test.jpg"), "image/*");
startActivity(intent);
答案 1 :(得分:1)
首先我将图像保存到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:"));
//