gpt4 book ai didi

java - 提取加载完整图像的路径 Recyclerview Cardview

转载 作者:行者123 更新时间:2023-12-01 19:01:14 26 4
gpt4 key购买 nike

下面的代码从数据库中提取图像并将其显示在回收器 View 上。完整图像的代码不显示完整图像。我怀疑我无法获取要解析为 FullImageViewe.java 的确切 URL。

如何解析点击的图像/文本网址以在此处显示完整图像?所有依赖关系都正常。

Recyclerview Cardview

Full Image View

主要 Activity

package com.ny.fetchallimages;

import android.content.Intent;
import android.os.Bundle;
import org.json.JSONArray;
import java.util.ArrayList;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import java.util.List;

import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

List<DataAdapter> ListOfdataAdapter;

RecyclerView recyclerView;

String HTTP_JSON_URL = "http://ny.com/uploaddownload/fetchallimages.php";

String Image_URL_JSON = "image_data";

String Image_Name_JSON = "image_tag";

String Image_Time_JSON = "time";

JsonArrayRequest RequestOfJSonArray ;

RequestQueue requestQueue ;

View view ;

int RecyclerViewItemPosition ;

RecyclerView.LayoutManager layoutManagerOfrecyclerView;

RecyclerView.Adapter recyclerViewadapter;

ArrayList<String> ImageTitleNameArrayListForClick;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ImageTitleNameArrayListForClick = new ArrayList<>();

ListOfdataAdapter = new ArrayList<>();

recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);

recyclerView.setHasFixedSize(true);

layoutManagerOfrecyclerView = new LinearLayoutManager(this);

recyclerView.setLayoutManager(layoutManagerOfrecyclerView);

JSON_HTTP_CALL();

// Implementing Click Listener on RecyclerView.
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {

GestureDetector gestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() {

@Override public boolean onSingleTapUp(MotionEvent motionEvent) {

return true;
}

});
@Override
public boolean onInterceptTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {
view = Recyclerview.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
if(view != null && gestureDetector.onTouchEvent(motionEvent)) {
//Getting RecyclerView Clicked Item value.
RecyclerViewItemPosition = Recyclerview.getChildAdapterPosition(view);
Intent intent = new Intent(MainActivity.this, FullImageViewer.class );
intent.putExtra("img", "HTTP_JSON_URL");
startActivity(intent);

}
return false;
}
@Override
public void onTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {

}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
}
public void JSON_HTTP_CALL(){
RequestOfJSonArray = new JsonArrayRequest(HTTP_JSON_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
ParseJSonResponse(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});
requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(RequestOfJSonArray);
}
public void ParseJSonResponse(JSONArray array){
for(int i = 0; i<array.length(); i++) {

DataAdapter GetDataAdapter2 = new DataAdapter();

JSONObject json = null;
try {
json = array.getJSONObject(i);

GetDataAdapter2.setImageTitle(json.getString(Image_URL_JSON));
// Adding image title name in array to display on RecyclerView click event.
ImageTitleNameArrayListForClick.add(json.getString(Image_URL_JSON));
GetDataAdapter2.setImageUrl(json.getString(Image_URL_JSON));
} catch (JSONException e) {
e.printStackTrace();
}
ListOfdataAdapter.add(GetDataAdapter2);
}
recyclerViewadapter = new RecyclerViewAdapter(ListOfdataAdapter, this);
recyclerView.setAdapter(recyclerViewadapter);
}
}

完整图像查看器

package com.ny.fetchallimages;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;

public class FullImageViewer extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullimageviewer);
final ImageView img = findViewById(R.id.img);
String url = (String) getIntent().getSerializableExtra("img");

//Glide library to load image from URL
Glide.with(getApplicationContext())
.asBitmap()
.load(url)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
img.setImageBitmap(resource);
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {

}
});
}
}

完整图像查看器.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="@color/color1">

<ImageView
android:id="@+id/img"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true" />
</RelativeLayout>

最佳答案

我想我知道这里发生了什么。您使用的链接“http://ny.com/uploaddownload/fetchallimages.php ”采用 HTTP 格式,而不是 https,并且谷歌在其应用程序中停止支持 HTTP,直到您在应用程序 list 中澄清这一点。可以使用以下方法来完成:

android:usesCleartextTraffic="true"

在你的android list 中有点像这样:

AndroidManifest.xml:

<manifest>
<application

android:allowBackup="true"
android:icon="@drawable/appicon"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
</manifest>

我在开发我的应用程序时也遇到了这个问题,并找到了这个解决方案 here它还有另一个更安全的解决方案,但我无法让它发挥作用。

希望我有帮助。 (顺便说一句,我也有一个问题,你看起来足够熟练,所以请 have a look at it

关于java - 提取加载完整图像的路径 Recyclerview Cardview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59636262/

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