簡體   English   中英

Android:無法從Android設備上的內部存儲訪問圖像文件

[英]Android: Cannot access an image file from the internal storage on Android device

我試圖將相機拍攝的圖像保存到設備的內部存儲中,然后將其用作ImageView,但是我無法訪問該文件。 這都是出於教育目的。 我有一個按鈕可以將相機應用程序稱為子活動。

public void addListener() {
    Button b = (Button)findViewById(R.id.snap);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            File picturesDirectory;
            picturesDirectory = getFilesDir();
            imageFile = new File(picturesDirectory, "passspoints_image");

            Log.d(DEBUGTAG, imageFile.getAbsolutePath());

            Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
            startActivityForResult(i, PHOTO_TAKEN);
        }
    });
}

然后,我嘗試訪問該文件。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case PHOTO_TAKEN:
            Log.d(DEBUGTAG, imageFile.getAbsolutePath());
            Bitmap photo = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
            try {
                photo = rotateImageIfRequired(photo, Uri.fromFile(imageFile));
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(photo != null) {
                ImageView imageView = (ImageView)findViewById(R.id.imageView);
                imageView.setImageBitmap(photo);
            } else {
                Toast.makeText(this, "Unable to read photo file", Toast.LENGTH_LONG).
                        show();
            }
            break;
    }
}

這是debug標記的路徑:

/data/data/tk.crackedbunny.www.takingphotostest/files/passspoints_image

這是錯誤消息:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /data/data/tk.crackedbunny.www.takingphotostest/files/passspoints_image: open failed: ENOENT (No such file or directory)

在此之前,我設法使用外部存儲來執行此操作,但是如果外部存儲不可用,我想使用內部存儲。

謝謝。

passspoints_image是圖片名稱嗎? 如何為其添加擴展名,例如passspoints_image.jpg

還是您在AndroidManifest.xml文件中具有讀/寫權限?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM