簡體   English   中英

嘗試將圖像保存到圖庫

[英]Trying to save image to gallery

我是Android開發的新手,嘗試將圖像保存到圖庫時有些卡住。 我已經按照https://developer.android.com/training/camera/photobasics上的指南進行操作,並且在我嘗試使用getExternalStoragePublicDirectory將圖像保存到手機上的圖庫之前,它一直工作正常。

當我使用getExternalFilesDir()時,所有工作正常,但是當我嘗試使用getExternalStoragePublicDirectory將圖片獲取到畫廊時,我得到了NullPointerException。 我已經向清單添加了WRITE_EXTERNAL_STORAGE權限。

我曾嘗試閱讀有關此問題的其他文章,但還沒有發現對這個問題的任何啟示。

這是我的代碼。 如果annyone可以幫助我找出導致NullPointerException異常的原因,我將不勝感激。

    private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}



private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File

        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

這是我在Logcat中的錯誤:

    08-02 20:04:12.920 17455-17455/com.example.android.fishlist E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.fishlist, PID: 17455
java.lang.NullPointerException
    at java.io.File.<init>(File.java:282)
    at com.example.android.fishlist.NewCatch.galleryAddPic(NewCatch.java:192)
    at com.example.android.fishlist.NewCatch.access$200(NewCatch.java:45)
    at com.example.android.fishlist.NewCatch$2.onClick(NewCatch.java:101)
    at android.view.View.performClick(View.java:6891)
    at android.widget.TextView.performClick(TextView.java:12651)
    at android.view.View$PerformClick.run(View.java:26083)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

我認為這行代碼會使您的應用崩潰

File f = new File(mCurrentPhotoPath); // mCurrentPhotoPath is null

從更改代碼

// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
);

// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(storageDir.getPath() + File.separator + imageFileName + ".jpg");

注意:如果您的應用程序在Android 6或更高版本上運行,請確保處理運行時權限。

暫無
暫無

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

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