문제

getExternalFilesDir()에 저장된 이미지가 있고 Android Gallery (CooliRIS)에서 이러한 이미지를 표시하려고합니다. 지금 나는 이것을 해왔다 :

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(imgPath,"image/*");
startActivity(intent);
.

그러나 아무 일도 일어나지 않습니다. setDataAndType을 다음과 같이 변경했습니다.

intent.setDataAndType(Uri.fromFile(new File(imgPath)),"image/*");
. 이 방법으로 작동하지만 갤러리가 검은 색 화면에서 이미지를 보여주기 위해 5-10 초가 걸리지 않습니다.

이 또는 더 나은 접근 방식을 해결하기 위해서

도움이 되었습니까?

해결책

파일 컨텐츠 제공자를 구현함으로써 5-10 초 지연

를 피할 수 있습니다.
import java.io.File;
import java.io.FileNotFoundException;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;

public class FileContentProvider extends ContentProvider {
    private static final String AUTHORITY = "content://com.yourprojectinfo.fileprovider";

    public static Uri constructUri(String url) {
        Uri uri = Uri.parse(url);
        return uri.isAbsolute() ? uri : Uri.parse(AUTHORITY + url);
    }

    public static Uri constructUri(File file) {
        Uri uri = Uri.parse(file.getAbsolutePath());
        return uri.isAbsolute() ? uri : Uri.parse(AUTHORITY
                + file.getAbsolutePath());
    }

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode)
            throws FileNotFoundException {
        File file = new File(uri.getPath());
        ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file,
                ParcelFileDescriptor.MODE_READ_ONLY);
        return parcel;
    }

    @Override
    public boolean onCreate() {
        return true;
    }

    @Override
    public int delete(Uri uri, String s, String[] as) {
        throw new UnsupportedOperationException(
                "Not supported by this provider");
    }

    @Override
    public String getType(Uri uri) {
        return "image/jpeg";
    }

    @Override
    public Uri insert(Uri uri, ContentValues contentvalues) {
        throw new UnsupportedOperationException(
                "Not supported by this provider");
    }

    @Override
    public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
        throw new UnsupportedOperationException(
                "Not supported by this provider");
    }

    @Override
    public int update(Uri uri, ContentValues contentvalues, String s,
            String[] as) {
        throw new UnsupportedOperationException(
                "Not supported by this provider");
    }

}
.

다음

를 호출 할 수 있습니다.
Uri uri = FileContentProvider.constructUri(file);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(uri,"image/*");
startActivity(intent);
.

이것은 이상한 대안이지만 URI를 사용하여 Android가 이미지를 열 때 어떻게 할 수 있는지와 관련이 있다고 생각합니다.그들의 OpenFile (URI URI, String Mode) 메서드는 잘못 / 고장 / 제대로 URI를 해결할 수 없습니다. 나는 실제로 100 % 확실하지는 않지만이 해결 방법을 효과적으로 발견했습니다.

매니페스트에서 공급자에게 등록하는 것을 잊지 마십시오

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top