如何制作照片并将其保存到图像持有者

时间:2015-11-12 23:31:10

标签: java android image android-intent camera

我需要制作一张照片并将其保存到图像库中。考虑一个带有1个按钮和1个占位符的空白活动。我需要运行相机程序并将其保存到占位符。尝试了几个我可以理解的样本,但没有一个,我在分配给占位符时得到空指针。到目前为止,这是我的活动:

public class MarkerInfo extends AppCompatActivity {
    int TAKE_PHOTO_CODE = 0;
    public static int count = 0;
    Uri outputFileUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_marker_info);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
        File newdir = new File(dir);
        newdir.mkdirs();
        Button capture = (Button) findViewById(R.id.CameraButton);
        capture.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                // here,counter will be incremented each time,and the picture taken by camera will be stored as 1.jpg,2.jpg and likewise.
                count++;
                String file = dir + count + ".jpg";
                File newfile = new File(file);
                try {
                    newfile.createNewFile();
                } catch (IOException e) {
                }

                outputFileUri = Uri.fromFile(newfile);

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



                startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
            }
        });
    }



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

        if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
            Log.d("CameraDemo", "Pic saved");
            ImageSwitcher switcher = new ImageSwitcher(getApplicationContext());
            switcher.setImageURI(outputFileUri);


        }
    }

这是我得到的最接近的。相机发射和拍照。我不确定我是否正确分配。它只需要一张照片,我需要公开的URI,所以我可以稍后将它保存到SQLite。对于首发显示会做。有任何想法吗? (代码不是我的,我在Android中不是那么高级) 编辑:照片必须在地图标记上设置,所以我需要唯一的名称,所以我可以单独获取标记。将几张照片放入水平幻灯片中也是一种选择。我的观点是代码必须是可重用的。我也不知道在照片已经保存的时候在OnResume()中添加什么,所以它出现在持有者中

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.example.fixxxer.mapapplication/com.example.fixxxer.mapapplication.MarkerInfo}: java.lang.NullPointerException

0 个答案:

没有答案