简体   繁体   English

错误:构造函数文件(URI)未定义

[英]Error: The Constructor File(URI) is undefined

I'm trying to write an activity that takes a picture and saves the data and image to the sd card where I can read the image data and output it. 我正在尝试编写一个活动,该活动需要拍照,然后将数据和图像保存到SD卡中,以便在其中读取图像数据并将其输出。 But, I'm getting an error "Constructor File(Uri) is undefined.") in the showPhoto() method. 但是,我在showPhoto()方法中收到错误消息“未定义构造函数File(Uri)。”。 Can anyone help?? 有人可以帮忙吗?

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);

        }

    }

尝试这样做:

File imageFile = new File(photoUri.getPath());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM