从parse.com检索图像文件并在图像视图中显示

时间:2016-06-16 23:12:40

标签: android image parse-platform

我正在尝试从parse.com下载图像,并在扩展AppCompatActivity的Activity中的图像视图中显示。

我从许多搜索中获得了此代码:

ParseImageView mImage = (ParseImageView) findViewById(R.id.image);

    ParseObject object = new ParseObject("Appetizers"); // class name
    ParseFile postImage = object.getParseFile("imageFiles"); // column name
    String imageUrl = postImage.getUrl() ;//live url
    Uri imageUri = Uri.parse(imageUrl);

   Picasso.with(getBaseContext()).load(imageUri.toString()).into(mImage);

My parse class

任何帮助都将不胜感激。

文件格式:jpg

3 个答案:

答案 0 :(得分:0)

这是我的解决方案。在我的活动中,扩展AppCompatActivity,这是在ImageView中下载图像的代码:

    ImageLoader.ImageCache imageCache = new BitmapLruCache();
    ImageLoader imageLoader = new ImageLoader(Volley.newRequestQueue(getApplicationContext()), imageCache);
    NetworkImageView headshot = (NetworkImageView) findViewById(R.id.speaker_headshot);
    headshot.setImageResource(R.drawable.loading);
    headshot.setImageUrl("http://www.anydomain.com/anyimage.jpg", imageLoader);

如您所见,您需要在项目中添加一个名为 BitmapLruCache.java 的文件。以下是它的全部内容:

public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache {

public BitmapLruCache() { this(getDefaultLruCacheSize()); }

public BitmapLruCache(int sizeInKiloBytes) { super(sizeInKiloBytes); }

@Override
protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight() / 1024; }

@Override
public Bitmap getBitmap(String url) { return get(url); }

@Override
public void putBitmap(String url, Bitmap bitmap) { put(url, bitmap); }

public static int getDefaultLruCacheSize() {
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;
    return cacheSize;
}
}

最后,要实现这一点,您必须在布局中设置一个特殊的ImageView XML

            <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/speaker_headshot"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:scaleType="centerCrop"/>

编辑:要完成所有这些工作,您必须导入:

1)Volley库。在Android Studio上执行此操作:菜单文件/项目结构/模块 - 应用程序/选项卡依赖项。在那里添加(+符号)库依赖项并查找或搜索 com.android.volley:volley:1.0.0 。而已。 OR 简单包含在build.gradle(模块应用)文件中:

dependencies {
compile 'com.android.volley:volley:1.0.0'
}

如果您已经有“依赖项”部分,那么只需在其中包含该行。

2)将 disklrucache-2.0.2.jar 的副本放入“libs”文件夹(从此处获取.jar https://search.maven.org/remote_content?g=com.jakewharton&a=disklrucache&v=LATEST)或在其中插入另一个“compile”指令你的“依赖”:

compile 'com.jakewharton:disklrucache:2.0.2'

答案 1 :(得分:0)

尝试这种方式。

picasso

你没有为picasso lib设置List<Order>占位符以获取清除捕获内存。

答案 2 :(得分:0)

我通过调试和设置日志来解决这个问题......

在发送课程中:

ParseObject po = mAppetizers.get(position); // get position
String ID = po.getObjectId().toString();

Intent intent = new Intent(Appetizer.this, AppetizerRecipe.class);

intent.putExtra("ID", ID);
startActivity(intent);

在接收者的课程中:

final ParseImageView mImage = (ParseImageView) findViewById(R.id.image);
String ID = getIntent().getStringExtra("ID");

        ParseQuery<ParseObject> getimage = new ParseQuery<>("Appetizers");
    getimage.addAscendingOrder("appetizer");
    getimage.whereEqualTo("ID", ID);
        Log.d("AppetizerRecipe2", "object: " + ID);
    getimage.getInBackground(ID, new GetCallback<ParseObject>() {
        @Override
        public void done(ParseObject object, ParseException e) {
            if (e == null) {
                Log.v("what is e?", "e = " + e);
                // success
                final ParseFile fileObject = (ParseFile)object.get("imageFiles");
                fileObject.getDataInBackground(new GetDataCallback() {
                    public void done(byte[] data, ParseException e) {
                        if (e == null) {
                            Log.d("test", "We've got data in data.");
                            // use data for something
                            Log.d("test", "Get your image..." + fileObject.getUrl());
                    Picasso.with(getBaseContext()).load(fileObject.getUrl()).placeholder
                            (R.drawable.ic_launcher).into(mImage);

                        } else {
                            Log.d("test", "There was a problem downloading the data.");
                        }
                    }
                });

            } else {
                // fail
                Log.d("test", "Error Message..." + e.getMessage());
            }
        }
    });