1

FileProvider로 앱을 빌드하고 가져온 후에 이미지를 저장하고 싶습니다. 갤러리에서 이미지를 찾을 수 없습니다. Android Studio 튜토리얼에서이 소스 코드를 찾았습니다. 나는 그 문제가 뭔지 모른다. 내가 사용하는 디버거를 시도하고 createFile() 올바른지 생각합니다. 나는 또한 비트 맵 작품을 가지고있다. 내가 찍은 이미지를 표시 할 수는 있지만 갤러리에 이미지를 추가 할 수는 없습니다.안드로이드 용 FileProvider 7.1.1을 사용하여 카메라로 사진을 찍은 후에 갤러리에 파일을 저장할 수 없습니다.

내 Manifest.xml

<provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="com.temp.test" 
     android:exported="false" 
     android:grantUriPermissions="true"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/file_paths"/> 
</provider> 

이 있습니다 그리고 file_paths.xml에 나는

<?xml version="1.0" encoding="utf-8"?> 
<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
     <external-path name="external" path="Android/data/com.temp.test/files/Pictures" /> 
</paths> 

을 내가 활동

private String mCurrentPhotoPath; 
private ImageView mImageView; 
private ImageButton StartCameraBtn; 
private File photoFile = null; 

//requestCode 
private static final int REQUEST_IMAGE_CAPTURE = 1; 

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

    mImageView = (ImageView) findViewById(R.id.imageView); 

    StartCameraBtn = (ImageButton) findViewById(R.id.StartCamera); 

    StartCameraBtn.setOnClickListener(this); 
} 
public void onClick(View view) 
{ 
    clearAllFocus(); 
    switch (view.getId()) 
    { 
     case R.id.StartCamera: 
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      if (takePictureIntent.resolveActivity(getPackageManager()) != null) 
      { 
       try 
       { 
        photoFile = createFile(); 
       } 
       catch (IOException e) 
       { 
        e.printStackTrace(); 
       } 
       if(photoFile != null){ 
        Uri photoURI = FileProvider.getUriForFile(this, 
          "com.temp.test", 
          photoFile); 
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
       } 
      } 
      break; 
... 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    mImageView.setImageBitmap(null); 

    switch (requestCode) 
    { 
     case REQUEST_IMAGE_CAPTURE: 
      if (resultCode == RESULT_OK) 
      { 
       setPic(); 
       galleryAddPic(); 
      } 
      break; 
    } 
} 
private File createFile() throws IOException 
{ 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp + "_"; 
    File storageDir = getExternalFilesDir(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 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); 
} 
private void setPic() 
{ 
    // Get the dimensions of the View 
    int targetW = mImageView.getWidth(); 
    int targetH = mImageView.getHeight(); 

    // Get the dimensions of the bitmap 
    BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bmOptions.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 
    int photoW = bmOptions.outWidth; 
    int photoH = bmOptions.outHeight; 

    // Determine how much to scale down the image 
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH); 

    // Decode the image file into a Bitmap sized to fill the View 
    bmOptions.inJustDecodeBounds = false; 
    bmOptions.inSampleSize = scaleFactor; 
    bmOptions.inPurgeable = true; 

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 
    mImageView.setImageBitmap(bitmap); 
} 

도와주세요를 작성하는 방법이되고있다 ! 고맙습니다!

나는 galleryaddpic()을 테스트하기 위해 디버거를 사용하며 URI가 성공적으로 액세스되었지만 왜 갤러리에 추가 할 수 없는지 모르겠다. 안드로이드 VM 디렉토리에서 이미지 파일을 찾을 수 없습니다. 이 디버그 로그입니다 :

https://i.stack.imgur.com/JN9uJ.png

내 domian로 chickendinner을 사용하고, 그리고 계속 내 응용 프로그램의 이름입니다.

감사합니다.

+0

없이 작동 public readable internal memory directories를 사용


은 [이] (HTTPS를보십시오.com/questions/19558188/photo-do-not-show-up-to-gallery) –

+0

죄송합니다 ..이 글을 읽었지만 얻지 못했습니다. 나는 그 게시물에 대한 답변에서 설명한 것과 같은 방법을 사용한다고 생각합니다. – Kurou

+0

@Aswin P Ashok :이 방법은'galleryAddPic()'이하는 것입니다. – k3b

답변

0

코드는

  • 이-dB 미디어를 통해 broadcst Intent.ACTION_MEDIA_SCANNER_SCAN_FILE
  • 생성에 새로운 사진을 추가 파일 시스템에 JPG 파일로 새로운 사진을 쓰기 처리하는 다른 응용 프로그램은 개인에 액세스 할 수 FileProvider//Android/data/com.temp.test/files/Pictures/... 콘텐츠를 통해 파일 - uri content://com.temp.test/...

나는 media-db 스캐너에 읽기 권한이 없다고 가정합니다. 파일 -URL을 통해 앱의 개인 데이터 디렉토리 Android/data/com.temp.test/files/Pictures에 저장되므로 새 사진을 media-db에 추가 할 수 없습니다.

Whatsapp 및 다른 앱은 수신/전송 사진을 공개 읽을 수있는 내부 메모리 (예 :/sdcard/PICTURES/WhatsApp /)에 저장합니다. 여기서 미디어 스캐너는 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE을 통해 file-uri를 통해 읽을 수 있습니다. 미디어 스캐너가 content: -uris 대신 file uri-s을 처리 할 수 ​​있는지

내가 알고이 donot : 당신이 시도 할 수 있습니다 :

private void galleryAddPic() 
{ 
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    // assume that mCurrentPhotoPath ="Android/data/com.temp.test/files/Pictures/myTestImage.jpg" 
    // can be accessed from outside as "content://com.temp.test/myTestImage.jpg" 
    Uri contentUri = Uri.parse("content://com.temp.test/myTestImage.jpg"); 
    mediaScanIntent.setData(contentUri); 
    this.sendBroadcast(mediaScanIntent); 
} 

이 작동하는지 알려 주시기 바랍니다.

그래도 작동하지 않으면 수동으로 "content : //com.temp.test/..."항목을 미디어 데이터베이스에 삽입 할 수 있습니다. // 유래하십시오 fileprovider

+0

안녕하세요 k3b, 내용의 경우 : -uris, 나는 미디어 스캐너가 wokring하기 때문에 그것을 처리 할 수 ​​없다고 생각한다. 안드로이드 7.1.1 위에있는 안드로이드 7.0 @ k3b – Kurou

+0

Uri contentUri = Uri.parse ("content : //com.temp.test/myTestImage.jpg"); 앱에 비공개 URI에 대한 액세스 권한이 없으므로이 행은 작동하지 않습니다. – Kurou