0

这让我发疯,因为当我在 android 中创建一张图片并将其保存在我的摩托罗拉 G 上时,它在图库中显示正常。但是当我在三星 Galaxy S3 上执行代码时,它只显示一个名为“Camera”的文件夹,其文件类型未知。

正如您在我的漂亮意大利面条代码中看到的那样,我创建了一个文件:

 //Insert an image and create a thumbnail for it.
 // 
 // @param cr
 //            The content resolver to use
 // @param source
 //            The stream to use for the image
 // @param title
 //            The name of the image
 // @param description
 //            The description of the image
 // @return The URL to the newly created image, or <code>null</code> if 
 //        the image failed to be stored for any reason.
 //
public final String insertImage(ContentResolver cr, Bitmap source,
        String title, String description, Long time) {
    ContentValues values = new ContentValues();
    values.put(Images.Media.TITLE, title);
    values.put(Images.Media.DESCRIPTION, description);
    values.put(Images.Media.MIME_TYPE, "image/jpeg");
    values.put(Images.Media.DATE_TAKEN, time);

    String stringUrl = null; /* value to be returned */

    OutputStream imageOut = null;
    try {
        valPub.url = cr.insert(Media.EXTERNAL_CONTENT_URI, values);
        try {
            imageOut = cr.openOutputStream(valPub.url);
            valPub.strUltimaFoto = valPub.url.getPath() + "/" + title;
        } catch (Exception e) {
            // Esto resuelve el problema de las cámaras Samsung
            stringUrl = valPub.pathSamsung; 
            File mDir = new File(stringUrl);
            if (!mDir.exists()) {
                mDir.mkdirs();
            }
            stringUrl = stringUrl + title;
            valPub.strUltimaFoto = stringUrl;
            File m_fil = new File(stringUrl);
            valPub.url = Uri.fromFile(m_fil);
            imageOut = new FileOutputStream(m_fil);
        }
        try {
            source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut);
            // bmpFoto = Bitmap.createBitmap(source);

        } catch (Exception e) {
        } finally {
            imageOut.close();
        }

        long id=0;
        try {
            id = ContentUris.parseId(valPub.url);
        }catch(Exception nEx) {
            Cursor mCursorLoader;
            int mColumnIndex;
             // Initializing CursorLoader
            mCursorLoader = initializeCursorLoader();
            mColumnIndex = mCursorLoader.getColumnIndex(MediaStore.Images.Media._ID);

            // Go thru all the images in the device (EXTERNAL_CONTENT_URI)
            for (int i = 0; i < mCursorLoader.getCount(); i++) {
                mCursorLoader.moveToPosition(i);
                id = mCursorLoader.getInt(mColumnIndex);
            }

        }

        // Wait until MINI_KIND thumbnail is generated.
        Bitmap miniThumb = null;
        try {
            miniThumb = Images.Thumbnails.getThumbnail(cr, id,
                    Images.Thumbnails.MINI_KIND, null);
        } catch (Exception e) {

        }
        if (miniThumb == null) {
            // Trick samsung
            galleryAddPic(valPub.url);
        }
        // This is for backward compatibility.
        @SuppressWarnings("unused")
        Bitmap microThumb = null;
        try {
            microThumb = StoreThumbnail(cr, miniThumb, id, 50F, 50F,
                    Images.Thumbnails.MICRO_KIND, title);
        } catch (Exception e) {
        }
    } catch (Exception e) {}

    if (valPub.url != null) {
        stringUrl = valPub.url.toString();
    }

    return stringUrl;
}

我试图用这个刷新画廊:

public void galleryAddPic(Uri picUri) {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, picUri);
        sendBroadcast(mediaScanIntent);
    }

......但它没有奏效。这似乎与异步任务有关,但我不知道如何修复它。

剩下的代码:

private Cursor initializeCursorLoader() {
        String[] COLUMNS = {
            MediaStore.Images.Thumbnails._ID, MediaStore.Images.Media.DATA
    };

    CursorLoader cursorLoader = new CursorLoader(
    this, // Context
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // Uri
    COLUMNS, // Projection
    null, // Selection
    null, // Selection Args

    // Sort Order: DESC = newest first
    // Sort Order: ASC = oldest first

    MediaStore.Images.Media.DATE_ADDED + " DESC");

    // *** NOTE ***
    // With:
    //
    // MediaStore.Images.Media.DATE_ADDED + " ASC"
    //
    // It runs just fine (MediaStore returns 'null' for invalid thumbnails)
    // The problem seems to reside on the " DESC" tag.
    //
    // How bizarre is that?

    return cursorLoader.loadInBackground();
}




// public static Uri getContentUri(String volumeName) {
// return Uri.parse("content://mtp/" + volumeName + "/video/media");
// }

private final Bitmap StoreThumbnail(ContentResolver cr, Bitmap source,
        long id, float width, float height, int kind, String title) {
    // create the matrix to scale it
    Matrix matrix = new Matrix();

    float scaleX = width / source.getWidth();
    float scaleY = height / source.getHeight();

    matrix.setScale(scaleX, scaleY);

    Bitmap thumb = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
            source.getHeight(), matrix, true);

    ContentValues values = new ContentValues(4);
    values.put(Images.Thumbnails.KIND, kind);
    values.put(Images.Thumbnails.IMAGE_ID, (int) id);
    values.put(Images.Thumbnails.HEIGHT, thumb.getHeight());
    values.put(Images.Thumbnails.WIDTH, thumb.getWidth());

    valPub.url = cr.insert(Images.Thumbnails.EXTERNAL_CONTENT_URI, values);
    if (valPub.url == null) {
        valPub.url = cr.insert(Media.INTERNAL_CONTENT_URI, values);
    }
    OutputStream thumbOut = null;
    try {
        thumbOut = cr.openOutputStream(valPub.url);
    } catch (Exception e) {
    }
    try {
        thumb.compress(Bitmap.CompressFormat.JPEG, 100, thumbOut);
        thumbOut.close();
        return thumb;
    } catch (FileNotFoundException ex) {
        return null;
    } catch (IOException ex) {
        return null;
    }
}

正如我之前所说,这是一个糟糕的意大利面条代码。我试图让它首先运行,我已经改变了很多次,没有任何运气。请随时提出任何改进建议。

4

1 回答 1

0

最后,我已经解决了。我更改了代码以执行 ALWAYS galleryAdPic,因为它刷新了三星设备上的图库:

public void galleryAddPic(Uri picUri) {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, picUri);
    sendBroadcast(mediaScanIntent);
}
于 2016-01-22T11:10:07.970 回答