来自SQLite的Android URI未显示在ImageView中

时间:2015-05-25 20:21:50

标签: android sqlite android-imageview

我有一个应用程序,它从手机摄像头拍摄照片并将路径作为TEXT存储在sqlite数据库中。我正在从数据库中检索字符串并解析它,并且由于某种原因它没有显示在ImageView中。以下是所有相关代码。

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

        editTextName = (EditText) findViewById(R.id.editTextName);
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextBirthdate = (EditText) findViewById(R.id.editTextBirthdate);
        editTextAge = (EditText) findViewById(R.id.editTextAge);
        imageViewStudentImage=(ImageView)findViewById(R.id.imageViewStudent);

        _Student_Id = 0;
        Intent intent = getIntent();
        _Student_Id = intent.getIntExtra("student_Id", 0);
        StudentRepo repo = new StudentRepo(this);
        Student student;
        student = repo.getStudentById(_Student_Id);

        editTextBirthdate.setText(student.bd);
        editTextName.setText(student.name);
        editTextEmail.setText(student.email);

//Display Image Here
        if (student.imagepath !=null) imageViewStudentImage.setImageURI(Uri.parse(student.imagepath));

        editTextAge.setText(CalculateAge(ConvertDate(student.bd)).toString());

    }

URI是file:///storage/emulated/0/Pictures/MyCameraApp/IMG_20150525_124520.jpg

有趣的是我在手机上的Gallery应用程序中找不到图像,但我可以使用我的文件浏览器应用找到。图像在手机上显示正确。

以下是存储URI的代码

public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

private void Save(){
        if (!editTextName.getText().toString().isEmpty()) {

            StudentRepo repo = new StudentRepo(this);
            Student student = new Student();

            student.imagepath = fileUri.toString();

            if (!editTextBirthdate.getText().toString().isEmpty()) {
                student.bd = editTextBirthdate.getText().toString();
            } else {
                student.bd = "";
            }

            if (!editTextEmail.getText().toString().isEmpty()) {
                student.email = editTextEmail.getText().toString();
            } else {
                student.email = "";
            }

            student.name = editTextName.getText().toString();
            student.student_ID = _Student_Id;

            if (_Student_Id == 0) {
                _Student_Id = repo.insert(student);

                Toast.makeText(this, "New Student Inserted", Toast.LENGTH_SHORT).show();
            } else {

                repo.update(student);
                Toast.makeText(this, "Student Record updated", Toast.LENGTH_SHORT).show();
            }
        }
    }

private void TakePicture(){
        // create Intent to take a picture and return control to the calling application
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

        // start the image capture Intent
        startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }

    /** Create a file Uri for saving an image or video */
    private static Uri getOutputMediaFileUri(int type){
        return Uri.fromFile(getOutputMediaFile(type));
    }

    /** Create a File for saving an image or video */
    private static File getOutputMediaFile(int type){
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "MyCameraApp");
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".jpg");
        } else if(type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_"+ timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

有人可以帮我弄清楚为什么ImageView不会显示图像吗?

编辑:

我刚刚发现了这个......

05-25 13:42:37.954  27938-27938/com.x.xxx W/ImageView﹕ Unable to open content: file:///storage/emulated/0/Pictures/MyCameraApp/IMG_20150525_124520.jpg
    java.io.FileNotFoundException: /storage/emulated/0/Pictures/MyCameraApp/IMG_20150525_124520.jpg: open failed: ENOENT (No such file or directory)
            at libcore.io.IoBridge.open(IoBridge.java:456)
            at java.io.FileInputStream.<init>(FileInputStream.java:76)
            at java.io.FileInputStream.<init>(FileInputStream.java:103)
            at android.content.ContentResolver.openInputStream(ContentResolver.java:650)
            at android.widget.ImageView.resolveUri(ImageView.java:778)
            at android.widget.ImageView.setImageURI(ImageView.java:440)
            at com.xx.xx.StudentDetail.onCreate(StudentDetail.java:60)
            at android.app.Activity.performCreate(Activity.java:6221)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2728)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
     Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
            at libcore.io.Posix.open(Native Method)
            at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
            at libcore.io.IoBridge.open(IoBridge.java:442)
            at java.io.FileInputStream.<init>(FileInputStream.java:76)
            at java.io.FileInputStream.<init>(FileInputStream.java:103)
            at android.content.ContentResolver.openInputStream(ContentResolver.java:650)
            at android.widget.ImageView.resolveUri(ImageView.java:778)
            at android.widget.ImageView.setImageURI(ImageView.java:440)
            at com.xx.xx.StudentDetail.onCreate(StudentDetail.java:60)
            at android.app.Activity.performCreate(Activity.java:6221)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2728)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
05-25 13:42:37.954  27938-27938/com.xx.xxI/System.out﹕ resolveUri failed on bad bitmap uri: file:///storage/emulated/0/Pictures/MyCameraApp/IMG_20150525_124520.jpg

显然它无法找到文件夹和/或图像。处理这个问题的最佳方法是什么?据我所知,将图像存储为BLOBS也不是一个好主意。

1 个答案:

答案 0 :(得分:0)

相对于“MyCameraApp”目录保存文件名是更好的选择。 您可以将目录设为

Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES).getAbsolutePath()
            + "/MyCameraApp/";

您可以在路径后附加文件名。 你可以这样打开它。

File file = new File(path) ;
        if (!file.exists()) {
            Toast.makeText(activity,R.string.file_deleted,Toast.LENGTH_SHORT).show();
            return;
        }
        Intent openIntent = new Intent(Intent.ACTION_VIEW);
        openIntent.setDataAndType(Uri.fromFile(file), file.getMimeType());
        PackageManager manager = activity.getPackageManager();
        List<ResolveInfo> infos = manager.queryIntentActivities(openIntent, 0);
        if (infos.size() > 0) {
            getContext().startActivity(openIntent);
        } else {
            Toast.makeText(activity,R.string.no_application_found_to_open_file,Toast.LENGTH_SHORT).show();
        }
相关问题