ANDROID :从解析中检索图像到 android activity
ANDROID :Retrieving image from parse to an android activity
我创建了一个 ListView。当我单击 ListView 的单个项目时,我调用 activity 来显示单个项目视图。正在解析 TextView 数据。但是当我试图从解析中调用图像时它没有加载(图像托管在一个单独的在线位置)。如果有人能告诉我如何在这里加载图像??
my file url
url = new URL("file location " + statusObject.getString("image"));
edited code
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);
}
}
在 Manifest.xml
中定义 <uses-permission android:name="android.permission.INTERNET" />
。
然后使用 Picasso
或 Gilde
并从它们各自的实例中解析图像,并传递您正在从解析中查询的 image URL。这些库将解析 ImageView 中的 URL 和 return 图像。
其他类似问题:
Load image from url
How to load an ImageView by URL in Android?
finally this code is working after editing some codes form the link in the answer . by this code you can
call images dynamically for each of the Object 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
我创建了一个 ListView。当我单击 ListView 的单个项目时,我调用 activity 来显示单个项目视图。正在解析 TextView 数据。但是当我试图从解析中调用图像时它没有加载(图像托管在一个单独的在线位置)。如果有人能告诉我如何在这里加载图像??
my file url
url = new URL("file location " + statusObject.getString("image"));
edited code
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);
}
}
在 Manifest.xml
中定义 <uses-permission android:name="android.permission.INTERNET" />
。
然后使用 Picasso
或 Gilde
并从它们各自的实例中解析图像,并传递您正在从解析中查询的 image URL。这些库将解析 ImageView 中的 URL 和 return 图像。
其他类似问题: Load image from url
How to load an ImageView by URL in Android?
finally this code is working after editing some codes form the link in the answer . by this code you can call images dynamically for each of the Object 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