通过android中的内容提供程序访问手机图像

发布于 2024-09-03 06:53:47 字数 233 浏览 6 评论 0原文

我需要示例代码或教程来通过内容提供商访问手机图像/媒体?

我知道以下内容,接下来怎么办?

ContentResolver cr = mContext.getContentResolver();
Cursor cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);

I need a sample code or tutorial for accessing phone images/media through content provider ?

I know the following, what next ?

ContentResolver cr = mContext.getContentResolver();
Cursor cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷情妓 2024-09-10 06:53:47

使用 CursorLoader:

(通过在 AppCompatActivity 中实现 LoaderManager.LoaderCallbacks

private void loadPhotosFromPhone() {
    getSupportLoaderManager().initLoader(0, null, this);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = {MediaStore.Images.Media.DATA};
    return new CursorLoader(this, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

    ArrayList<String> alstPhotos = new ArrayList<>();

    for (data.moveToLast(); !data.isBeforeFirst(); data.moveToPrevious()){
        String photoPath = data.getString(0);
        alstPhotos.add(photoPath);
    }

    // Use alstPhotos
}

这将异步加载设备上的所有图像。

Using a CursorLoader:

(by implementing LoaderManager.LoaderCallbacks<Cursor> in your AppCompatActivity)

private void loadPhotosFromPhone() {
    getSupportLoaderManager().initLoader(0, null, this);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = {MediaStore.Images.Media.DATA};
    return new CursorLoader(this, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

    ArrayList<String> alstPhotos = new ArrayList<>();

    for (data.moveToLast(); !data.isBeforeFirst(); data.moveToPrevious()){
        String photoPath = data.getString(0);
        alstPhotos.add(photoPath);
    }

    // Use alstPhotos
}

This would load all the Images on the Device, asynchronously.

天生の放荡 2024-09-10 06:53:47

我创建了一个辅助类,它将从用户设备收集所有图像路径。如果您愿意,可以使用它。对于您的信息,加载图像可能会很慢,因此如果您通过后台线程或加载器加载它们会很好。

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import java.util.ArrayList;

/**
 * @author hendrawd on 10/26/16
 */

public class ImagePathProvider {

    /**
     * Getting All Images Path.
     *
     * @param context the activity
     * @return ArrayList with images Path
     */
    public static ArrayList<String> getAllImagesPath(Context context) {
        ArrayList<String> listOfAllImages = new ArrayList<>();
        listOfAllImages.addAll(getExternalImagesPath(context));
        listOfAllImages.addAll(getInternalImagesPath(context));
        return listOfAllImages;
    }

    /**
     * Getting All External Images Path.
     *
     * @param context the context
     * @return ArrayList with external images Path
     */
    private static ArrayList<String> getExternalImagesPath(Context context) {
        return getImagesPathFromUri(context, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    }

    /**
     * Getting All Internal Images Path.
     *
     * @param context the context
     * @return ArrayList with internal images Path
     */
    private static ArrayList<String> getInternalImagesPath(Context context) {
        return getImagesPathFromUri(context, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    }

    private static ArrayList<String> getImagesPathFromUri(Context context, Uri uri) {
        Cursor cursor;
        int column_index_data;
        ArrayList<String> listOfAllImages = new ArrayList<>();
        String absolutePathOfImage;
        String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
        cursor = context.getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null) {
            column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            while (cursor.moveToNext()) {
                absolutePathOfImage = cursor.getString(column_index_data);
                listOfAllImages.add(absolutePathOfImage);
            }
            cursor.close();
        }
        return listOfAllImages;
    }
}

I made a helper class that will collect all images path from user's device. You can use it if you want. For your info, it might be slow to load the images, so it will be good if you load them via background thread or loader.

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import java.util.ArrayList;

/**
 * @author hendrawd on 10/26/16
 */

public class ImagePathProvider {

    /**
     * Getting All Images Path.
     *
     * @param context the activity
     * @return ArrayList with images Path
     */
    public static ArrayList<String> getAllImagesPath(Context context) {
        ArrayList<String> listOfAllImages = new ArrayList<>();
        listOfAllImages.addAll(getExternalImagesPath(context));
        listOfAllImages.addAll(getInternalImagesPath(context));
        return listOfAllImages;
    }

    /**
     * Getting All External Images Path.
     *
     * @param context the context
     * @return ArrayList with external images Path
     */
    private static ArrayList<String> getExternalImagesPath(Context context) {
        return getImagesPathFromUri(context, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    }

    /**
     * Getting All Internal Images Path.
     *
     * @param context the context
     * @return ArrayList with internal images Path
     */
    private static ArrayList<String> getInternalImagesPath(Context context) {
        return getImagesPathFromUri(context, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    }

    private static ArrayList<String> getImagesPathFromUri(Context context, Uri uri) {
        Cursor cursor;
        int column_index_data;
        ArrayList<String> listOfAllImages = new ArrayList<>();
        String absolutePathOfImage;
        String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
        cursor = context.getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null) {
            column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            while (cursor.moveToNext()) {
                absolutePathOfImage = cursor.getString(column_index_data);
                listOfAllImages.add(absolutePathOfImage);
            }
            cursor.close();
        }
        return listOfAllImages;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文