ANDROID:从解析中检索图像到 android 活动
Posted
技术标签:
【中文标题】ANDROID:从解析中检索图像到 android 活动【英文标题】:ANDROID :Retrieving image from parse to an android activity 【发布时间】:2017-02-22 07:08:23 【问题描述】:我创建了一个 ListView。当我单击 ListView 的单个项目时,我正在调用一个活动来显示单个项目视图。 TextView 数据正在解析。但是当我试图从解析中调用图像时,它不会加载(图像托管在一个单独的在线位置)。如果有人可以请告诉如何在这里加载图像??
我的文件地址
url = new URL("file location " + statusObject.getString("image"));
修改后的代码
URL url = null;
try
url = new URL("https://www.gravatar.com/avatar/db5bfe41c9d21d3a5b4e5b5f6a2d776d?s=32&d=identicon&r=PG&f=1");
catch (MalformedURLException e1)
e1.printStackTrace();
Bitmap bmp = null;
try
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
catch (IOException e1)
e1.printStackTrace();
txtv2.setImageBitmap(bmp);
///////////////
单个项目视图
import android.content.*;
import android.net.*;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.*;
import android.view.View;
import android.widget.*;
import com.parse.*;
import com.parse.ParseException;
public class SingleItemView extends AppCompatActivity
String objectId;
protected TextView txtv;
protected TextView txtv1;
protected ImageView txtv2;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_item_view);
txtv =(TextView)findViewById(R.id.txt123);
txtv1 =(TextView)findViewById(R.id.txt1234);
txtv2 =(ImageView)findViewById(R.id.txt12345);
Intent i =getIntent();
objectId = i.getStringExtra("objectId");
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.getInBackground(objectId, new GetCallback<ParseObject>()
public void done(ParseObject object, ParseException e)
if (e == null)
String username = object.getString("firstname");
txtv.setText(username);
String position = object.getString("position");
txtv1.setText(position);
String img_ = object.getString("image");
txtv2.setImageURI(Uri.parse(img_));
else
// something went wrong
);
片段文件
import android.content.*;
import android.os.Bundle;
import android.support.v4.app.*;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import java.util.ArrayList;
import java.util.List;
public class Individuals extends android.support.v4.app.ListFragment
implements FindCallback<ParseObject>
private List<ParseObject> mOrganization = new ArrayList<ParseObject>();
SearchView sv;
IndividualsAdaptor adaptor;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
return inflater.inflate(R.layout.individuals, container, false);
@Override
public void onViewCreated(View view, Bundle b)
super.onViewCreated(view, b);
sv = (SearchView) view.findViewById(R.id.ser1);
adaptor = new IndividualsAdaptor(getActivity(), mOrganization);
setListAdapter(adaptor);
ParseQuery.getQuery("_User").findInBackground(this);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener()
@Override
public boolean onQueryTextSubmit(String text)
return false;
@Override
public boolean onQueryTextChange(String text)
adaptor.getFilter().filter(text);
return true;
);
@Override
public void done(List<ParseObject> scoreList, ParseException e)
if (e == null)
Log.d("score", "Retrieved " + scoreList.size() + " _User");
mOrganization.clear();
mOrganization.addAll(scoreList);
((IndividualsAdaptor) getListAdapter()).updateBackupList(mOrganization);
((IndividualsAdaptor) getListAdapter()).notifyDataSetChanged();
else
Log.d("score", "Error: " + e.getMessage());
@Override
public void onListItemClick(android.widget.ListView l, android.view.View v, int position, long id)
super.onListItemClick(l, v, position, id);
ParseObject prs = mOrganization.get(position);
String objectId = prs.getObjectId();
Intent i = new Intent(getActivity(), SingleItemView.class);
i.putExtra("objectId", objectId);
startActivity(i);
【问题讨论】:
你是否在使用解析对象的检索中获取 URL 查看这个答案***.com/a/27793603/3310181 【参考方案1】:在Manifest.xml
中定义<uses-permission android:name="android.permission.INTERNET" />
。
然后使用Picasso
或Gilde
并从它们各自的实例中解析图像,并传递您从解析中查询的图像URL。这些库将解析 URL 并在 ImageView 中返回图像。
其他类似问题: Load image from url
How to load an ImageView by URL in Android?
【讨论】:
您的第一个链接在经过一些编辑后可以正常工作.. 但只有占位符图像才能告诉您如何加载动态图像检查我的问题我更新了@Sneh Pandya【参考方案2】:在从答案中的链接编辑一些代码后,这段代码终于可以工作了。通过此代码,您可以 为每个对象 ID动态调用图像。
URL url = null;
try
url = new URL("image url" + object.getString("image"));
catch (MalformedURLException e1)
e1.printStackTrace();
Bitmap bmp = null;
try
bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
catch (IOException e1)
e1.printStackTrace();
txtv2.setImageBitmap(bmp);
// something went wrong
【讨论】:
以上是关于ANDROID:从解析中检索图像到 android 活动的主要内容,如果未能解决你的问题,请参考以下文章
从 android 客户端将图像存储到 Blobstore 并检索 blobkey 并上传 url 以存储在 Datastore 中。 - 盖伊
Android:无法从mysql数据库中检索图像URL并将其显示在imageView中
android 从 MySql 数据库中检索 blob 图像