Java 安卓可以';t显示通过插座传输的一些图像
我的应用程序向局域网中的其他设备发送一些文本和图像。看起来图像传输和接收成功(我没有任何异常),但不知何故,有些图像在CardViewAdapter的imageView中显示不好或根本不显示 当我收到图像时,我创建文件并用createFile方法保存它。我还传输图像类型并使用此扩展名保存它。我不知道这取决于什么,但我无法解决它 通过tcp发送图像的类:Java 安卓可以';t显示通过插座传输的一些图像,java,android,image,imageview,Java,Android,Image,Imageview,我的应用程序向局域网中的其他设备发送一些文本和图像。看起来图像传输和接收成功(我没有任何异常),但不知何故,有些图像在CardViewAdapter的imageView中显示不好或根本不显示 当我收到图像时,我创建文件并用createFile方法保存它。我还传输图像类型并使用此扩展名保存它。我不知道这取决于什么,但我无法解决它 通过tcp发送图像的类: public class ImageTransferHandler implements Runnable{ priva
public class ImageTransferHandler implements Runnable{
private static final String TAG = "ImageTransferHandler";
private static final int SERVER_PORT = 8700;
private Socket socket;
private Context context;
private String hostIpAddress;
private Bar bar;
public ClientThreadProcessor(String hostIpAddress, Context context, Bar bar){
this.context = context;
this.hostIpAddress = hostIpAddress;
this.Bar = bar;
}
private Socket getSocket(String hostIpAddress) {
InetAddress serverAddr = InetAddress.getByName(hostIpAddress);
socket = new Socket(serverAddr, SERVER_PORT);
return socket;
}
private void closeSocket(Socket socket) {
socket.close();
}
@Override
public void run() {
socket = getSocket(hostIpAddress);
OutputStream outputStream = socket.getOutputStream();
PrintWriter output = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(outputStream)), true);
output.println(bar.getImageInfo().isCamera());
Uri imageUri = Uri.parse(bar.getImageInfo().getPath());
String mimeType = ImagePicker.getMimeType(context, imageUri);
String ext = mimeType.substring(mimeType.lastIndexOf("/") + 1);
if(ext == null) ext = "bmp";
output.println(ext);
BufferedOutputStream bout = new BufferedOutputStream(outputStream);
String realPath = ImagePicker.getRealPathFromUri(context, imageUri);
InputStream input = new FileInputStream(realPath);
byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer)) != -1) {
bout.write(buffer);
}
input.close();
bout.close();
Log.d(TAG, "Image sent over network and stream is closed.");
output.close();
closeSocket(socket);
}
}
通过tcp接收图像的类
public class ImageReceiveHandler implements Runnable{
private static String TAG = "ImageReceiveHandler";
private Socket socket;
private Context context;
private BufferedReader inputBufferedReader;
private PrintWriter outputPrintWriter;
private Bar Bar;
public ClientHandler(Socket socket, Context context){
this.socket = socket;
this.context = context;
}
@Override
public void run() {
InputStream input = socket.getInputStream();
inputBufferedReader = new BufferedReader(
new InputStreamReader(input));
outputPrintWriter = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
boolean isCamera = Boolean.valueOf(inputBufferedReader.readLine());
String ext = inputBufferedReader.readLine();
File imageFile = ImagePicker.createFile(context, ImagePicker.isExternalStorageWritable(), ext);
if (imageFile != null) {
Uri imageUri = Uri.fromFile(imageFile);
Log.d(TAG, "imageUri for received image: " + imageUri.toString());
BufferedInputStream bufin = null;
FileOutputStream output = null;
bufin = new BufferedInputStream(input);
output = new FileOutputStream(imageFile);
byte[] buffer = new byte[1024];
int len;
while((len = bufin.read(buffer)) != -1){
output.write(buffer);
output.flush();
}
output.close();
bufin.close();
Log.d(TAG, "Image is received and saved. File length: " + imageFile.length());
ImageInfo info = new ImageInfo(imageUri.toString(), isCamera);
Bar = new Bar(info);
addNotification(Bar, hostAddress);
}
else
Log.d(TAG, "ImagePicker.createFile returns NULL");
inputBufferedReader.close();
outputPrintWriter.close();
}
public File createFile(Context context, boolean external, String ext) {
File direct;
if(external)
direct = new File(Environment.getExternalStorageDirectory() + "/myo_images");
else
direct = new File(context.getFilesDir() + "/myo_images");
if (!direct.exists()) {
File imagesDir;
if(external)
imagesDir = new File("/sdcard/myo_images/");
else
imagesDir = new File("/myo_images");
imagesDir.mkdirs();
}
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File file = null;
if(external)
file = File.createTempFile("myo_" + timestamp, "." + ext, direct);
else
file = File.createTempFile("myo_" + timestamp, "." + ext, direct);
return file;
}
}
应可视化图像的适配器类
public class BarsCardViewAdapter extends RecyclerView.Adapter<BarsCardViewAdapter.CardViewHolder> {
private List<BarData> activeBars;
private final static String TAG = "CARDVIEW_ADAPTER";
public BarsCardViewAdapter(List<BarData> Bars){
activeBars = Bars;
}
public static class CardViewHolder extends RecyclerView.ViewHolder {
private ActiveBarsListItemBinding listItemBinding;
public CardViewHolder(View v) {
super(v);
listItemBinding = DataBindingUtil.bind(v);
}
public ActiveBarsListItemBinding getBinding(){
return listItemBinding;
}
}
@Override
public BarsCardViewAdapter.CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.bars_list_item, parent, false);
CardViewHolder holder = new CardViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(final BarsCardViewAdapter.CardViewHolder holder, final int position) {
holder.getBinding().imageView.setVisibility(View.GONE);
final Bar bar = activeBars.get(position);
final ImageInfo imageInfo = bar.getImageInfo();
final LinearLayout rLayout = holder.getBinding().listItemLayout;
ImageInfo imageInfo = BarData.getImageInfo();
Log.d(TAG, "imagePath: " + Uri.parse(imageInfo.getPath()));
holder.getBinding().imageView.setVisibility(View.VISIBLE);
Picasso.with(rLayout.getContext()).
load(imageInfo.getPath()).
transform(new CropSquareTransformation()).
into(holder.getBinding().imageView);
holder.getBinding().setVariable(BR.Bar, bar);
holder.getBinding().executePendingBindings();
}
@Override
public int getItemCount() {
return activeBars.size();
}
}
公共类BarsCardViewAdapter扩展了RecyclerView.Adapter{
私有列表活动条;
私有最终静态字符串标记=“CARDVIEW\u适配器”;
公共条形图(列表条形图){
活动条=条;
}
公共静态类CardViewHolder扩展了RecyclerView.ViewHolder{
私有ActiveBarsListItemBinding列表项绑定;
公共名片夹(视图v){
超级(五);
listItemBinding=DataBindingUtil.bind(v);
}
public ActiveBarsListItemBinding getBinding(){
返回列表项绑定;
}
}
@凌驾
公共条形图CARDVIEWAWADAPTER.CardViewHolder onCreateViewHolder(视图组父级,int viewType){
视图v=LayoutInflater.from(parent.getContext())
.充气(R.layout.bars\u list\u项目,父项,错误);
CardViewHolder=新的CardViewHolder(v);
报税表持有人;
}
@凌驾
BindViewHolder上的公共无效(最终BarscardViewwaDapter.CardViewHolder,最终int位置){
holder.getBinding().imageView.setVisibility(View.GONE);
最终条=活动条。获取(位置);
final ImageInfo ImageInfo=bar.getImageInfo();
final LinearLayout rllayout=holder.getBinding().listItemLayout;
ImageInfo ImageInfo=BarData.getImageInfo();
Log.d(标记“imagePath:+Uri.parse(imageInfo.getPath()));
holder.getBinding().imageView.setVisibility(View.VISIBLE);
Picasso.with(rLayout.getContext())。
加载(imageInfo.getPath())。
转换(新的CropSquareTransformation()。
进入(holder.getBinding().imageView);
holder.getBinding().setVariable(BR.Bar,Bar);
holder.getBinding().executePendingBindings();
}
@凌驾
public int getItemCount(){
返回activebar.size();
}
}
您试图读入二进制数据,但使用的读取器根据指定的字符集,通过某种转换将字节转换为字符:
inputBufferedReader = new BufferedReader(
new InputStreamReader(input));
这将使用系统的字符集打开读卡器(例如UTF8
或cp1252
)
不使用读写器,而是使用流,在本例中,BufferedInputStream和BufferedOutputStream以及
字节的读/写块
在这里,我使用DataInput/OutputStream实现了数据传输。数据传输似乎正常,但现在它不显示任何图像。你能告诉我我的代码有什么问题吗?(其余的都一样)
我收到这个错误消息,我想我关闭了所有东西。为什么会出现“命中文件结束”错误
09-12 20:25:00.005 13426-14473/mattoncino.pollo E/JHEAD:hit file end
09-12 20:25:00.032 13426-14473/mattoncino.pollo W/BitmapFactory:解码器JPEG解码失败
发送者线程
public class ImageTransferHandler implements Runnable{
private static final String TAG = "ImageTransferHandler";
private static final int SERVER_PORT = 8700;
private Socket socket;
private Context context;
private String hostIpAddress;
private Bar bar;
@Override
public void run() {
socket = getSocket(hostIpAddress);
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataOutputStream.writeBoolean(bar.getImageInfo().isCamera());
Uri imageUri = Uri.parse(bar.getImageInfo().getPath());
String mimeType = ImagePicker.getMimeType(context, imageUri);
String ext = mimeType.substring(mimeType.lastIndexOf("/") + 1);
if(ext == null) ext = "bmp";
dataOutputStream.writeUTF(ext);
String realPath = ImagePicker.getRealPathFromUri(context, imageUri);
File imageFile = new File(realPath);
byte[] byteArray = new byte[(int) imageFile.length()];
FileInputStream fis = new FileInputStream(imageFile);
fis.read(byteArray);
fis.close();
dataOutputStream.writeInt(byteArray.length);
dataOutputStream.write(byteArray, 0, byteArray.length);
dataOutputStream.flush();
dataOutputStream.close();
closeSocket(socket);
}
}
public class ImageReceiveHandler implements Runnable{
private static String TAG = "ImageReceiveHandler";
private Socket socket;
private Context context;
private DataOutputStream dataOutputStream;
private DataInputStream dataInputStream;
private Bar Bar;
@Override
public void run() {
dataInputStream = new DataInputStream(socket.getInputStream());
boolean isCamera = dataInputStream.readBoolean();
String ext = dataInputStream.readUTF();
File imageFile = ImagePicker.createFile(context, ImagePicker.isExternalStorageWritable(), ext);
if (imageFile != null) {
Uri imageUri = Uri.fromFile(imageFile);
Log.d(TAG, "imageUri for received image: " + imageUri.toString());
int len = dataInputStream.readInt();
byte[] buffer = new byte[len];
dataInputStream.read(buffer, 0, len);
FileOutputStream fileOut = new FileOutputStream(imageFile);
fileOut.write(buffer);
fileOut.close();
Log.d(TAG, "Image is received and saved. File length: " + imageFile.length());
ImageInfo info = new ImageInfo(imageUri.toString(), isCamera);
Bar = new Bar(info);
addNotification(Bar, hostAddress);
}
else
Log.d(TAG, "ImagePicker.createFile returns NULL");
dataInputStream.close();
}
接收器线程
public class ImageTransferHandler implements Runnable{
private static final String TAG = "ImageTransferHandler";
private static final int SERVER_PORT = 8700;
private Socket socket;
private Context context;
private String hostIpAddress;
private Bar bar;
@Override
public void run() {
socket = getSocket(hostIpAddress);
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataOutputStream.writeBoolean(bar.getImageInfo().isCamera());
Uri imageUri = Uri.parse(bar.getImageInfo().getPath());
String mimeType = ImagePicker.getMimeType(context, imageUri);
String ext = mimeType.substring(mimeType.lastIndexOf("/") + 1);
if(ext == null) ext = "bmp";
dataOutputStream.writeUTF(ext);
String realPath = ImagePicker.getRealPathFromUri(context, imageUri);
File imageFile = new File(realPath);
byte[] byteArray = new byte[(int) imageFile.length()];
FileInputStream fis = new FileInputStream(imageFile);
fis.read(byteArray);
fis.close();
dataOutputStream.writeInt(byteArray.length);
dataOutputStream.write(byteArray, 0, byteArray.length);
dataOutputStream.flush();
dataOutputStream.close();
closeSocket(socket);
}
}
public class ImageReceiveHandler implements Runnable{
private static String TAG = "ImageReceiveHandler";
private Socket socket;
private Context context;
private DataOutputStream dataOutputStream;
private DataInputStream dataInputStream;
private Bar Bar;
@Override
public void run() {
dataInputStream = new DataInputStream(socket.getInputStream());
boolean isCamera = dataInputStream.readBoolean();
String ext = dataInputStream.readUTF();
File imageFile = ImagePicker.createFile(context, ImagePicker.isExternalStorageWritable(), ext);
if (imageFile != null) {
Uri imageUri = Uri.fromFile(imageFile);
Log.d(TAG, "imageUri for received image: " + imageUri.toString());
int len = dataInputStream.readInt();
byte[] buffer = new byte[len];
dataInputStream.read(buffer, 0, len);
FileOutputStream fileOut = new FileOutputStream(imageFile);
fileOut.write(buffer);
fileOut.close();
Log.d(TAG, "Image is received and saved. File length: " + imageFile.length());
ImageInfo info = new ImageInfo(imageUri.toString(), isCamera);
Bar = new Bar(info);
addNotification(Bar, hostAddress);
}
else
Log.d(TAG, "ImagePicker.createFile returns NULL");
dataInputStream.close();
}
已编辑
public class ImageTransferHandler implements Runnable{
private static final String TAG = "ImageTransferHandler";
private static final int SERVER_PORT = 8700;
private Socket socket;
private Context context;
private String hostIpAddress;
private Bar bar;
@Override
public void run() {
socket = getSocket(hostIpAddress);
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataOutputStream.writeBoolean(bar.getImageInfo().isCamera());
Uri imageUri = Uri.parse(bar.getImageInfo().getPath());
String mimeType = ImagePicker.getMimeType(context, imageUri);
String ext = mimeType.substring(mimeType.lastIndexOf("/") + 1);
if(ext == null) ext = "bmp";
dataOutputStream.writeUTF(ext);
String realPath = ImagePicker.getRealPathFromUri(context, imageUri);
File imageFile = new File(realPath);
byte[] byteArray = new byte[(int) imageFile.length()];
FileInputStream fis = new FileInputStream(imageFile);
fis.read(byteArray);
fis.close();
dataOutputStream.writeInt(byteArray.length);
dataOutputStream.write(byteArray, 0, byteArray.length);
dataOutputStream.flush();
dataOutputStream.close();
closeSocket(socket);
}
}
public class ImageReceiveHandler implements Runnable{
private static String TAG = "ImageReceiveHandler";
private Socket socket;
private Context context;
private DataOutputStream dataOutputStream;
private DataInputStream dataInputStream;
private Bar Bar;
@Override
public void run() {
dataInputStream = new DataInputStream(socket.getInputStream());
boolean isCamera = dataInputStream.readBoolean();
String ext = dataInputStream.readUTF();
File imageFile = ImagePicker.createFile(context, ImagePicker.isExternalStorageWritable(), ext);
if (imageFile != null) {
Uri imageUri = Uri.fromFile(imageFile);
Log.d(TAG, "imageUri for received image: " + imageUri.toString());
int len = dataInputStream.readInt();
byte[] buffer = new byte[len];
dataInputStream.read(buffer, 0, len);
FileOutputStream fileOut = new FileOutputStream(imageFile);
fileOut.write(buffer);
fileOut.close();
Log.d(TAG, "Image is received and saved. File length: " + imageFile.length());
ImageInfo info = new ImageInfo(imageUri.toString(), isCamera);
Bar = new Bar(info);
addNotification(Bar, hostAddress);
}
else
Log.d(TAG, "ImagePicker.createFile returns NULL");
dataInputStream.close();
}
Bar是一个表示我的主要对象的类,它还包含imageinfo。有一些文本而不是imageInfo。但是我认为这个类或者它的用法没有问题 addNotification只是一个创建通知的方法,其目的是将图像信息传递给包含适配器的活动 ImageInfo是一个用于保存图像信息的类:一个字符串路径和一个布尔值,用于标记是否从相机获取图像(在调整图像大小时,其他方法使用了此信息,但对于我们的问题,它与此无关) 我认为文件传输是可以的。发送方和接收方的文件大小相同。ext是图像的扩展 发件人日志: Image len:23262 Mime类型:Image/jpeg ext:jpeg 接收器日志: 接收并保存图像。图像长度:23262外部:jpeg
final类ImageInfo实现可打包、可序列化{
私有字符串路径;
私有最终布尔isCamera;
公共图像信息(字符串路径,布尔值isCamera){
this.path=path;
this.isCamera=isCamera;
}
公共字符串getPath(){
返回路径;
}
公共void setPath(字符串路径){
this.path=path;
}
公共布尔值isCamera(){
返回isCamera;
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效书面包裹(包裹,内部i){
包裹写线(路径);
parcel.writeByte((字节)(isCamera?1:0));
}
公共静态最终包裹。创建者
=新的Parcelable.Creator(){
public ImageInfo createFromParcel(Parcel in){返回新的ImageInfo(in);}
public ImageInfo[]newArray(int size){返回新的ImageInfo[size];}
};
公共图像信息(包裹){
path=parcel.readString();
isCamera=parcel.readByte()!=0;
}
}
您只能从inputBufferedReader读取一行。只有一个ext。但缓冲读取器将具有buf