Android Studio - 从图库导入图像

时间:2018-03-15 06:06:02

标签: java android gallery

我正在创建活动,其中包含来自图库的图像(将图像拍摄到个人资料照片),但我有三个问题:

1)有些照片是重复的 2)如果我从SD卡显示图像,它加载大约30秒左右 3)有视频缩影

也许有人知道如何修复它?我想像Facebook,Instagram或Behance应用程序那样做

def main():
# Txt read

    input1 = open('text.txt', 'r').read().strip()
    l = input1.split("\n")
    #convert to int
    ll = [int(s) for s in l]
    print(ll)

main()

1 个答案:

答案 0 :(得分:1)

你的readImages方法有点矫枉过正,AFAIK从手机内存加载照片的最佳做法是这样的:

    private int imagesQuantity = 40;
    private Context ctx;
    private Cursor cursor;
    private ArrayList<Bitmap> photos = new ArrayList<>(imagesQuantity);
    private ArrayList<Uri> uris = new ArrayList<>(imagesQuantity);
    private ArrayList<Bitmap> photos = new ArrayList<>(imagesQuantity);

如上所述使用位图声明arrayList,并且读取方法应如下所示,有一些参数您可以像读取照片的顺序一样更改(DESC =降序最好在您的情况下我猜猜

  String[] projection = new String[]{
            MediaStore.Images.ImageColumns._ID,
            MediaStore.Images.ImageColumns.DATA,
            MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
            MediaStore.Images.ImageColumns.DATE_TAKEN,
            MediaStore.Images.ImageColumns.MIME_TYPE,
    };
    cursor = ctx.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
            null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
    cursor.moveToFirst();
    for (int i = 1; i <= imagesQuantity; i++) {
        Bitmap bmp = BitmapFactory.decodeFile(cursor.getString(1));
        Uri imageUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID)));
        photos.add(bmp);
        uris.add(imageUri);
        cursor.moveToNext();
    }
}