简体   繁体   English

在ImageView中显示从数据库下载的图像

[英]Displaying a Downloaded Image From Database in ImageView

I'm developing an Android app which allows users to upload/download images to and from a database (powered by Amazon AWS). 我正在开发一个Android应用程序,该应用程序允许用户在数据库中上载图像或从数据库中下载图像(由Amazon AWS提供支持)。 The problem I'm facing is that I can successfully download the files to a directory 我面临的问题是我可以成功将文件下载到目录中

/storage/emulated/0/data/myfile.jpg

But I cannot display them as a new ImageView. 但是我无法将它们显示为新的ImageView。

Here are my methods that deal with displaying the methods. 这是我处理显示方法的方法。 Note that RefreshFeedTask.downloadedFiles is a List of Bitmaps as shown here: 请注意,RefreshFeedTask.downloadedFiles是位图列表,如下所示:

 do {
        objectListing = s3Client.listObjects(listObjectsRequest);
        for (S3ObjectSummary objectSummary :
                objectListing.getObjectSummaries()) {

           keys.add(objectSummary.getKey());
        }
        listObjectsRequest.setMarker(objectListing.getNextMarker());
    } while (objectListing.isTruncated());

    Iterator<String> o = keys.iterator();

    while(o.hasNext())
    {

        String n = o.next();
        File file = new File(Environment.getExternalStorageDirectory()+"/data/", n);
        if(!file.exists())
        {
            try {
                file.createNewFile();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        TransferObserver observer = transferUtility.download(
                existingBucketName,
                n,
                file);

        Bitmap m = BitmapFactory.decodeFile(file.getAbsolutePath());

 private void refreshFeed()
{

    new RefreshFeedTask(this).start();
    for(Bitmap f : RefreshFeedTask.downloadedFiles)
    {
        displayImage(f);
    }

}

private void displayImage(Bitmap f){

    ImageView myImage = new ImageView(this);
    myImage.setImageBitmap(f);
    myImage.setVisibility(View.VISIBLE);
    Log.i("Background","Displaying file");

}

Any help is appreciated, as I am somewhat new to Android development, but not Java development. 感谢任何帮助,因为我是Android开发的新手,而不是 Java开发的新手。

One issue that you have here is Ownership. 您在这里遇到的一个问题是所有权。

Advise: That file you just downloaded to that location, your app may not own that location. 忠告:您刚刚下载到该位置的文件,您的应用可能不拥有该位置。 If this is a closed ecosystem where you are the sole user and owner of that file, you should probably create an application specific directory upon running the application. 如果这是一个封闭的生态系统,您是该文件的唯一用户和所有者,则可能应在运行应用程序时创建特定于应用程序的目录。

The best way to display something that you do not own in the Sdcard is to use the MediaStore API in Android to access them. 显示您在Sdcard中不拥有的东西的最好方法是使用Android中的MediaStore API来访问它们。 You need a URI to the right path and just doing an absolute path is not generally advised, especially for image assets. 您需要一个指向正确路径的URI,通常不建议只使用绝对路径,尤其是对于图像资产。

A good example of this is here: https://dzone.com/articles/displaying-images-sd-card 一个很好的例子在这里: https//dzone.com/articles/displaying-images-sd-card

Looks like you have not added you new ImageView to any view, try adding your new ImageView to any container to display it. 看起来您尚未将新ImageView添加到任何视图,请尝试将新ImageView添加到任何容器以显示它。

parentLayout.addView(theNewImageView);

Another tip: You can try Glide if you want to display many images more efficiently. 另一个提示:如果您想更有效地显示许多图像,可以尝试使用Glide

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM