java - Android camera2

java - Android camera2,第1张

我试图创建简单的相机应用程序。这是源代码:

package com.homeinc.basiccamera2.app;   
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;


public class MainActivity extends AppCompatActivity {

    public static final int CAPTURE_IMAGE_ACTIVITY_REQUST_CODE = 100;
    private static final int MEDIA_TYPE_IMAGE = 1;

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public  void takePicture(View view){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUST_CODE);
    }

    private Uri getOutputMediaFileUri(int type) {
        File imagesFolder = new File(Environment.getExternalStoragePublicDirectory(
                                     Environment.DIRECTORY_PICTURES),"myPics");
        if (!imagesFolder.exists()){
            if (!imagesFolder.mkdirs()){
                Log.d("BasicCamera2","cant create dir");
            }
        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File image = new File(imagesFolder.getPath() File.separator "IMG_" timeStamp ".jpg");
        Uri uriSavedImage = Uri.fromFile(image);
        return uriSavedImage;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUST_CODE && data != null){
            if (resultCode == RESULT_OK){
                Toast.makeText(this, "saved: "   data.getAction(), Toast.LENGTH_LONG).show();
            }
            else if (resultCode == RESULT_CANCELED){
                // canceled
                Toast.makeText(this, "saved: "   data.getAction(), Toast.LENGTH_LONG).show();
            } else {
                // fail
                Toast.makeText(this, "cant save: "   data.getAction(), Toast.LENGTH_LONG).show();
            }
        }
    }
}

查看onActivityResult方法它有3个参数,它应该从数据参数中的Intent接收数据,但它总是为null。请告诉我我做错了什么?

最佳答案:

1 个答案:

答案 0 :(得分:0)

我认为这样的事情(你可以只需要定义目录和文件名)应该为你做的伎俩。在给出文件的位置时,无需使用Intent来获取图片。

private void takePicture() {
    File newdir = new File(getImageDir());
    newdir.mkdirs();

    String file = getFileName();
    File newfile = new File(file);
    try {
        newfile.createNewFile();
    } catch (IOException e) {}

    Uri outputFileUri = Uri.fromFile(newfile);

    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

    startActivityForResult(cameraIntent, CAPTURE_IMAGE_ACTIVITY_REQUST_CODE);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUST_CODE && resultCode == RESULT_OK) {
        useFileName(getFileName());
    }
}
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复