【发布时间】:2016-09-13 03:28:43
【问题描述】:
我正在尝试学习如何在应用程序中使用相机,这就是我所达到的,这个想法是有一个打开相机的按钮,并且在我们拍照后照片会立即显示在屏幕上,第二个按钮来自早期版本,无法立即显示图片,必须单击才能显示。
无论如何,我的问题是这段代码没有在 android 6 上显示图片。在我的 android 5 设备上它工作正常。图片保存在“sdcard/camera_app/cam_image.jpg”路径中该按钮无法正常工作,所以我在想有关 imageview 的某些内容已从 android 5 更改为 6?问题几乎是如何使这项工作适用于 android 6 手机
public class Add_Comment_Picture extends AppCompatActivity {
static final int CAM_REQUEST = 1;
ImageView imageView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__comment__picture);
button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.imageView);
Button button2 = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_intent , CAM_REQUEST);
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String path = "sdcard/camera_app/cam_image.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
}
});
}
private File getFile()
{
File folder = new File("sdcard/camera_app");
if (!folder.exists())
{
folder.mkdir();
}
File image_file = new File(folder,"cam_image.jpg");
return image_file;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String path = "sdcard/camera_app/cam_image.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
super.onActivityResult(requestCode, resultCode, data);
}
}
【问题讨论】:
-
查看我的编辑以获取更多参考@Yuval Eliav
标签: android imageview android-camera onactivityresult