0

我花了很多时间试图解决这个微不足道的问题,但即使在阅读了许多教程和以前提出的问题之后,我似乎也陷入了困境。我是编程新手,但想变得更好,并相信从错误中学习是最好的方法。

有人可以帮助确定此代码的问题以及可以做些什么来允许设备相机拍摄的照片随后保存到手机上,而不是被丢弃和预览。

提前致谢!

相关类的 Java 代码:

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Activity_Camera extends Activity implements View.OnClickListener {

    public static final int cameraData = 1;

    ImageButton ib;
    ImageView iv;
    Intent i;
    Bitmap bmp;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.Info:

            Intent i = new Intent(this, Help.class);
            startActivity(i);

            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

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

    private void initialise() {
        iv = (ImageView) findViewById(R.id.ivPicReturn);
        ib = (ImageButton) findViewById(R.id.ibTakePic);

        ib.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.ibTakePic:
            i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            //
            i.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri());
            //
            startActivityForResult(i, cameraData);
            break;
        }
    }
    //
    private String getOutputMediaFileUri() {
        File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
        imagesFolder.mkdirs();   
        File image = new File(imagesFolder, "image.jpg");
        Uri uriSavedImage = Uri.fromFile(image);
        Intent imageIntent = null;
        imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);       
        return null;
    }
    //
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            bmp = (Bitmap) extras.get("data");
            iv.setImageBitmap(bmp);

        }

    }

}
4

1 回答 1

3

因为你在你的返回 return null;nullgetOutputMediaFileUri

试试这个

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.ibTakePic:
        i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        i.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri());
        startActivityForResult(i, cameraData);
        break;
    }
}

private Uri getOutputMediaFileUri() {
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
    imagesFolder.mkdirs();   
    File image = new File(imagesFolder, "image.jpg");
    return Uri.fromFile(image);
}
于 2013-03-06T02:56:46.463 回答