一个图像显示在我的应用上,另一个图像不显示,但两者都可以从浏览器访问。
我的免费主机中的这个没有显示在我的应用上。请注意,我无法从免费主机服务器上看到图像:
http://www.justedhak.comlu.com/images/uploaded_images.jpg
这是来自一个随机网站,并在应用程序上显示。
http://api.androidhive.info/json/movies/1.jpg
我的问题是,我上传图片的方式可能存在问题吗?或者可以是免费主机(000webhost)?请给我你的意见
我正在将图片上传到服务器,然后在活动中显示我可以看到url,但它没有显示在应用中。但是我还在应用程序中添加了另一个图像网址及其显示。
这是获取网址图片的代码
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String id = c.getString(TAG_ID);
String url = c.getString(TAG_PATH);
Listitem.add(new Listitem(id,url));
}
GridViewAdapter adapter = new GridViewAdapter(this, R.layout.grid_item_layout, Listitem);
// gridView.setAdapter(gridAdapter);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
public class GetDataJSON extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://justedhak.comlu.com/get-data.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
这是上传图片
}
public void onclick(View view)
{
Toast.makeText(AddImage.this, "Uploading Image", Toast.LENGTH_LONG).show();
upload();
Intent i = new Intent(this,
MainActivity.class);
startActivity(i);
}
public void upload()
{
Calendar thisCal = Calendar.getInstance();
thisCal.getTimeInMillis();
// android.util.Log.i("Time Class ", " Time value in millisecinds "+ thisCal);
// Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
// ByteArrayOutputStream stream = new ByteArrayOutputStream();
// bmp.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
Intent intent = getIntent();
String selectedImage= intent.getStringExtra("imagePath");
Uri fileUri = Uri.parse(selectedImage);
// Uri selectedImage = intent.getData();
System.out.println(fileUri);
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(fileUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bmp = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 30, stream);
byte[] byteArray = stream.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
imageview.setImageBitmap(bitmap);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
System.out.println(width);
System.out.println(height);
getResizedBitmap( bitmap, 200);
try {
stream.close();
stream = null;
} catch (IOException e) {
e.printStackTrace();
}
String image_str = Base64.encodeBytes(byteArray);
final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",image_str));
nameValuePairs.add(new BasicNameValuePair("caption",caption));
nameValuePairs.add(new BasicNameValuePair("name","je"));
nameValuePairs.add(new BasicNameValuePair("categorie",categorie));
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://justedhak.comlu.com/images/upload_image.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
final String the_string_response = convertResponseToString(response);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(AddImage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show();
}
});
}catch(final Exception e){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(AddImage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
System.out.println("Error in http connection "+e.toString());
}
}
});
t.start();
}
php文件
$con = mysqli_connect($host,$uname,$pwd,$db);
$description= $_GET['description'];
$categorie= $_GET['categorie'];
$imageurl="www.justedhak.comlu.com/images/uploaded_images.jpg";
$image=$_POST['image'];
$binary=base64_decode($image);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploaded_images.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
$sql = "insert into image (description,categorie,path) values ('$categorie','$description','$imageurl')";
if(mysqli_query($con,$sql)){
echo 'success';
}
else{
echo 'failure';
}
mysqli_close($con);
echo 'Image upload complete!!, Please check your php file directory……';
答案 0 :(得分:2)
我发现Picasso无法显示您的图片的主要原因是您的JSON中的图片Url不以http://
开头。
这是我的工作示例代码(仅用于获取图像Url并由Picasso显示,不包括图像上传)
public class MainActivity extends AppCompatActivity {
private final Context mContext = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView) findViewById(R.id.textView);
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
String url = "http://justedhak.comlu.com/get-data.php";
RequestQueue queue = Volley.newRequestQueue(mContext);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response != null && !response.isNull("result")) {
try {
JSONArray jsonArray = response.getJSONArray("result");
if (jsonArray != null && jsonArray.length() > 0) {
// get only one element for testing display image
JSONObject jsonObject = jsonArray.getJSONObject(1);
if (jsonObject != null && !jsonObject.isNull("path")) {
String imagePath = jsonObject.getString("path");
if (imagePath != null && !imagePath.isEmpty()) {
textView.setText(imagePath);
Picasso.with(mContext).load("http://" + imagePath).into(imageView);
}
}
}
} catch (JSONException e) {
textView.setText(e.toString());
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
textView.setText(error.toString());
}
});
queue.add(jsonObjectRequest);
}
}
以下是结果截图(您可以关注textView
的值,不带http://
前缀)
P / S: GET 请求也有效(您可以通过任何网络浏览器(例如Chrome)访问http://justedhak.comlu.com/get-data.php
进行检查)
总结一下,您将有两个选择:
http://
http://
启动,则必须在上面的示例代码中添加此前缀我认为您可以尝试使用HttpGet而不是HttpPost来获取Urls图像。我刚试过并得到以下结果;
{ “结果”:[{ “ID”: “1”, “姓名”:NULL, “路径”: “http://api.androidhive.info/json/movies/1.jpg”},{ “ID”: “2”, “名”:空, “路径”:“www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"28","name":null, “路径”:“www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"27","name":null,"path":"www.justedhak.comlu.com/images /uploaded_images.jpg"},{"id":"26","name":null,"path":"www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"25 “ ”名“:空, ”路径“:” www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"24","name":null,"path":"www .justedhak.comlu.com /图片/ uploaded_images.jpg “},{” ID “:” 23" , “名”:空, “路径”: “www.justedhak.comlu.com/images/uploaded_images.jpg”} { “ID”: “22”, “名”:空, “路径”: “www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"21","name”:空, “路径”:“www.justedhak.comlu.com/images/uploaded_images.jpg"},{"id":"20","name":null,"path":"www.justedhak.comlu.com /images/uploaded_images.jpg"},{"id":"19","name":null,"path":"www.justedhak.comlu.com/images/uploade d_images.jpg “},{” ID “:” 18" , “姓名”:NULL, “路径”: “www.justedhak.comlu.com/images/uploaded_images.jpg”}]}
此外,尝试使用HttpUrlConnection,OkHttp ...代替Apache HttpClient,因为它现在已弃用。
希望这有帮助!