简体   繁体   English

相机照片图像全尺寸imageview

[英]camera photo image full size imageview

I have a View with a button and an imageview. 我有一个带有按钮的视图和一个imageview。 There I want to take a large size photo with the android camera and set it in the imageview. 在那里,我想用Android相机拍摄一张大尺寸的照片,并将其设置在imageview中。 With the following Code I get no Image in the imageview. 使用以下代码,我在imageview中没有图像。 What is wrong in my code?Please help.Thank you for help 我的代码有什么问题?请帮助。谢谢您的帮助

package de.camera.test;
...

public class CameratestActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */

public File mTemp;
public ImageView image;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button cameraButton = (Button) findViewById(R.id.button);
    image = (ImageView) findViewById(R.id.imageview);
    cameraButton.setOnClickListener(this);
}

public void onClick(View v) {

    Intent camera = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    try {
        mTemp = File.createTempFile("myCameraShot", null);
        camera.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(mTemp));
        startActivityForResult(camera, 0);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (data == null)
        return;
    Bitmap b;
    try {
        b = Media.getBitmap(getContentResolver(), Uri.fromFile(mTemp));

        image.setImageBitmap(b);
        Drawable d = image.getDrawable();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

Layout main.xml 布局main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<ImageView
    android:id="@+id/imageview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />


</LinearLayout>

The documentation of File.createTempFile("myCameraShot", null) says that a temporary file is created in the default location for temporary files. File.createTempFile("myCameraShot", null)的文档说,在默认位置为临时文件创建了一个临时文件。 There is a good chance that this file is protected such that only your app has access to it. 该文件很有可能受到保护,因此只有您的应用程序可以访问它。

I think something you can try is specify a filepath which is on the SD card. 我认为您可以尝试的操作是指定SD卡上的文件路径。

I also suggest you look at this: Creating temporary files in Android 我也建议您看一下: 在Android中创建临时文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM