gpt4 book ai didi

android - 如何在mysql数据库中上传图片链接的android中显示图片

转载 作者:行者123 更新时间:2023-11-30 22:05:17 25 4
gpt4 key购买 nike

新闻节目.java

    package com.synergywebdesigners.nima;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.net.URL;


public class newsshow extends ArrayAdapter<String> {
private String[] ids;
private String[] ttitle;
private String[] tmessage;
private String[] tcreated;
private String[] picture;
private Bitmap[] bitmaps;
private Activity context;



public newsshow(Activity context, String[] ids, String[] title, String[] picture,Bitmap[] bitmaps, String[] message, String[] created) {
super(context, R.layout.list_view_layout, picture);
this.context = context;
this.ids = ids;
this.ttitle = title;
this.picture= picture;
this.bitmaps= bitmaps;
this.tmessage = message;
this.tcreated = created;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_view_layout, null, true);
/// TextView id = (TextView) listViewItem.findViewById(R.id.id);
TextView title = (TextView) listViewItem.findViewById(R.id.title);
ImageView image = (ImageView) listViewItem.findViewById(R.id.imageDownloaded);
TextView message = (TextView) listViewItem.findViewById(R.id.message);
TextView created = (TextView) listViewItem.findViewById(R.id.created);
//id.setText(ids[position]+".");
title.setText(ttitle[position]);
// spanned=Html.fromHtml(tmessage[position]);
image.setImageBitmap(Bitmap.createScaledBitmap(bitmaps[position],100,50,true));
message.setText(Html.fromHtml(tmessage[position]));
created.setText(tcreated[position]);
return listViewItem;
}
}

新闻.java

package com.synergywebdesigners.nima;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;



public class news extends AppCompatActivity{
public static final String JSON_URL = "http://www.synergywebdesigners.com/synergywebdesigners.com/ashish/nima/get_all_news.php";
private ListView listView;
// SharedPreferences pref;
TextView textView,textview2,textview3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
// pref = getSharedPreferences("news", MODE_PRIVATE);
final ListView list = (ListView) findViewById(R.id.listView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// String item = list.getItemAtPosition(position).toString();
textView = (TextView) view.findViewById(R.id.title);
textview2 = (TextView) view.findViewById(R.id.message);
textview3 = (TextView) view.findViewById(R.id.created);
String item2 = textView.getText().toString();
String item3 = textview2.getText().toString();
String item4 = textview3.getText().toString();
Intent i = new Intent(getApplicationContext(),newsview.class);
i.putExtra("item2",item2);
i.putExtra("item3",item3);
i.putExtra("item4",item4);

startActivity(i);
}
});
listView = (ListView) findViewById(R.id.listView);
sendRequest();
}
private void sendRequest() {
StringRequest stringRequest = new StringRequest(JSON_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(news.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});

com.android.volley.RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String json) {
ParseJSON pj = new ParseJSON(json);
pj.parseJSON();
newsshow cl = new newsshow(this, ParseJSON.ids, ParseJSON.ttitle, ParseJSON.picture,ParseJSON.bitmaps, ParseJSON.tmessage, ParseJSON.tcreated);
listView.setAdapter(cl);
}
}

解析器.java

package com.synergywebdesigners.nima;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
* Created by PC on 16-Dec-16.
*/

public class ParseJSON {
public static String[] ids;
public static String[] ttitle;
public static String[] tmessage;
public static String[] tcreated;
public static String[] picture;
public static Bitmap[] bitmaps;
public static final String JSON_ARRAY = "result";
public static final String KEY_ID = "id";
public static final String KEY_Title = "title";
public static final String KEY_Message = "message";
public static final String KEY_Created = "created";
public static final String KEY_IMAGE ="picture";
private JSONArray users = null;

private String json;

public ParseJSON(String json){
this.json = json;
try {
JSONObject jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);
} catch (JSONException e) {
e.printStackTrace();
}
}
private Bitmap getImage(JSONObject jo){
URL url = null;
Bitmap image = null;
try {
url = new URL(jo.getString(KEY_IMAGE));
image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return image;
}
protected void parseJSON(){
JSONObject jsonObject=null;

try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);
ids = new String[users.length()];
ttitle = new String[users.length()];
bitmaps = new Bitmap[users.length()];
picture = new String[users.length()];
tmessage = new String[users.length()];
tcreated = new String[users.length()];
for(int i=0;i<users.length();i++){
JSONObject jo = users.getJSONObject(i);
ids[i] = jo.getString(KEY_ID);
ttitle[i] = jo.getString(KEY_Title);
picture[i] = users.getJSONObject(i).getString(KEY_IMAGE);
bitmaps[i]= getImage(jo);
tmessage[i] = jo.getString(KEY_Message);
tcreated[i] = jo.getString(KEY_Created);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}

获取所有新闻.php

   <?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','mrecord');

$con = mysqli_connect(HOST,USER,PASS,DB);

$sql = "select * from news ORDER BY created DESC LIMIT 100";

$res = mysqli_query($con,$sql);

$result = array();

while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'title'=>$row[1],
'picture'=>$row[2],
'message'=>$row[3],
'created' =>$row[4]
));
}

echo json_encode(array('result'=>$result));

mysqli_close($con);
?>

新闻节目.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="10dp">

<!-- Title Label -->
<TextView
android:paddingLeft="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:textAllCaps="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold"
android:id="@+id/title"
android:gravity="center"/>
<ImageView android:id="@+id/imageDownloaded"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/abc_ic_menu_copy_mtrl_am_alpha"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/message"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/created"
android:layout_marginLeft="20dp"/>

</LinearLayout>

content_news.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_news"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="0dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_news">
<ScrollView android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:fillViewport="true">


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="1dp"
android:paddingBottom="16dp">

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:id="@+id/listView" />
</LinearLayout>
</ScrollView>
</RelativeLayout>

activity_news.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.synergywebdesigners.nima.news">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_news" />

</android.support.design.widget.CoordinatorLayout>

我想从 MySQL 服务器获取图像并在 android 中显示图像。但是每次都会创建 java 空指针异常。

最佳答案

Use Picasso for Image downloading and show in your ImageView

在你的 Gradle 中编译库:

compile 'com.squareup.picasso:picasso:2.5.2'

在您要设置图像的Activity中,喜欢:

Picasso.with(your_activity.this).load(image_url_from_server).into(your_imageView);

关于android - 如何在mysql数据库中上传图片链接的android中显示图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41976648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com