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을 사용하고, 그리고 계속 내 응용 프로그램의 이름입니다.
감사합니다.
없이 작동
public readable internal memory directories
를 사용은 [이] (HTTPS를보십시오.com/questions/19558188/photo-do-not-show-up-to-gallery) –
죄송합니다 ..이 글을 읽었지만 얻지 못했습니다. 나는 그 게시물에 대한 답변에서 설명한 것과 같은 방법을 사용한다고 생각합니다. – Kurou
@Aswin P Ashok :이 방법은'galleryAddPic()'이하는 것입니다. – k3b