如何显示,我只是从我的相机把我的Andr​​oid应用程序的图像? [英] How do I display an image that I just took from my camera in my android app?

查看:144
本文介绍了如何显示,我只是从我的相机把我的Andr​​oid应用程序的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图拍摄一张照片,并查看它在屏幕上用下面的code:

I am trying to take a picture and view it on the screen with the following code:

public class MainActivity extends ActionBarActivity {
    private static final int PICTURE_REQUEST_CODE = 100;
    public Button camera, gallery;
    private Uri fileUri;
    private static File mediaFile;
    ImageView image;
    public String fileName;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        System.out.println("Started Main Activity");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        camera = (Button) findViewById(R.id.button1);
        gallery = (Button) findViewById(R.id.button2);
        createListeners();
    }
    private static Uri getOutputMediaFile(){
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Mustache");
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg");

        return Uri.fromFile(mediaFile);
    }
    public void launchCamera(){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        fileUri = getOutputMediaFile(); // create a file to save the image
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(intent, PICTURE_REQUEST_CODE);

    }
    public void launchGallery(){
        Intent gallery = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        fileUri = getOutputMediaFile(); // create a file to save the image
        gallery.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(gallery, PICTURE_REQUEST_CODE);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
           super.onActivityResult(requestCode, resultCode, data);
               System.out.println(String.valueOf(requestCode));
               System.out.println(String.valueOf(resultCode));
               System.out.println(data);
               Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
               Uri contentUri = Uri.fromFile(mediaFile);
               mediaScanIntent.setData(contentUri);
               this.sendBroadcast(mediaScanIntent);

    }
    public static final int MEDIA_TYPE_VIDEO = 2;

    private void createListeners() {
        System.out.println("Started CreateListeners for Main Activity");
        camera.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                launchCamera();
            }

        });

        gallery.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                launchGallery();
            }
        });
    }


}

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    tools:context="com.example.mustache.MainActivity"
    tools:ignore="MergeRootFrame" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:text="@string/main_camera_button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:text="@string/main_gallery_button" />

</LinearLayout>

然而,应用程序拍照并保存到我的画廊,但随后返回到主屏幕,而不做任何事情。我如何让它显示照片?

However, the app takes a photo and saves it to my gallery, but then returns to the main screen without doing anything. How do I make it display the photo?

推荐答案

您需要的ImageView

public class MainActivity extends ActionBarActivity {
    private static final int PICTURE_REQUEST_CODE = 100;
    public Button camera, gallery;
    private Uri fileUri;
    private static File mediaFile;
    ImageView image;
    public String fileName;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        System.out.println("Started Main Activity");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        camera = (Button) findViewById(R.id.button1);
        gallery = (Button) findViewById(R.id.button2);
        createListeners();
    }
    private static Uri getOutputMediaFile(){
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Mustache");
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg");

        return Uri.fromFile(mediaFile);
    }
    public void launchCamera(){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        fileUri = getOutputMediaFile(); // create a file to save the image
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(intent, PICTURE_REQUEST_CODE);

    }
    public void launchGallery(){
        Intent gallery = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        fileUri = getOutputMediaFile(); // create a file to save the image
        gallery.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(gallery, PICTURE_REQUEST_CODE);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
           super.onActivityResult(requestCode, resultCode, data);
               System.out.println(String.valueOf(requestCode));
               System.out.println(String.valueOf(resultCode));
               System.out.println(data);
               Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
               Uri contentUri = Uri.fromFile(mediaFile);
               mediaScanIntent.setData(contentUri);
               this.sendBroadcast(mediaScanIntent);

    }
    public static final int MEDIA_TYPE_VIDEO = 2;

    private void createListeners() {
        System.out.println("Started CreateListeners for Main Activity");
        camera.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                launchCamera();
            }

        });

        gallery.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                launchGallery();
            }
        });
    }


}

这篇关于如何显示,我只是从我的相机把我的Andr​​oid应用程序的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆