2

我正在尝试编写一个拍照并将数据和图像保存到 SD 卡的活动,我可以在其中读取图像数据并将其输出。但是,我在 showPhoto() 方法中收到错误“构造函数文件(Uri)未定义。”)。有人可以帮忙吗??

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

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager.OnActivityResultListener;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class CallCamera extends Activity {


    private static final String TAG = "CallCamera";
    private static final int CAPTURE_IMAGE_ACTIVITY_REQ = 0;

    Uri fileUri = null;
    ImageView photoImage = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_camera);
        photoImage=(ImageView) findViewById(R.id.photo_image);
        Button callCameraButton = (Button)findViewById(R.id.button_callcamera);
            callCameraButton.setOnClickListener(new View.OnClickListener(){

                public void onClick(View view){
                    Intent i= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File file = getOutputPhotoFile();
                    fileUri=Uri.fromFile(getOutputPhotoFile());
                    i.putExtra(MediaStore.EXTRA_OUTPUT,  fileUri);
                    startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ); //returns photo file to activity when camera is done
                }
                    //store photo taken on SD card
                private File getOutputPhotoFile() {
                    File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),getPackageName());

                    if (!directory.exists()){
                        if(!directory.mkdirs()){
                            Log.e(TAG,"Failed to create storage directory.");
                            return null;
                        }
                    }

                    //set photo name with standard time linked and store on sdCard in standard picture directory
                    String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss",Locale.ENGLISH).format(new Date());
                    return new File(directory.getPath() + File.separator + "IMG_"+timeStamp+".jpg");
                }


            protected void onActivityResult (int requestCode, int resultCode, Intent data){
                if (requestCode==CAPTURE_IMAGE_ACTIVITY_REQ){
                    if(resultCode==RESULT_OK){
                        Uri photoUri = null;
                    if (data==null){
                        //confirming image save
                        Toast.makeText(CallCamera.this,  "Image saved successfully",  Toast.LENGTH_LONG).show();
                        photoUri=fileUri;
                    } else {
                        photoUri = data.getData();
                        Toast.makeText(CallCamera.this, "imaged saved successfully in: " + data.getData(), Toast.LENGTH_LONG).show();
                        }
                    photoUri = data.getData();
                    showPhoto(photoUri);

                    } else if (resultCode==RESULT_CANCELED) {
                        Toast.makeText( CallCamera.this,"Cancelled", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(CallCamera.this, "Callout for image capture failed!", Toast.LENGTH_LONG).show();
                    }
                }
            }

            });


    }
    protected void showPhoto(Uri photoUri) {
        File imageFile = new File(photoUri);
        if (imageFile.exists()){
            Drawable oldDrawable = photoImage.getDrawable();if(oldDrawable !=null) { ((BitmapDrawable)oldDrawable).getBitmap().recycle();

            }
        }

        if (imageFile.exists()){
            Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
            BitmapDrawable drawable = new BitmapDrawable(this.getResources(),bitmap);
            photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);;
            photoImage.setImageDrawable(drawable);

        }

    }
4

1 回答 1

5

尝试这样做:

File imageFile = new File(photoUri.getPath());
于 2013-08-31T17:46:02.390 回答