问题
I'm using Android Studio to build an application. In this app, I want to get an image in a specific folder from a linux host using Jsch sftp
, and set this new picture to an already-existing imageview.
But unfortunately the picture doesn't show up.
The following are the codes for this so far:
MainActivity.java
package com.example.picture_controller_2;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.ChannelSftp;
import android.view.View;
import android.os.AsyncTask;
import android.widget.ImageView;
import android.widget.Toast;
public class Button2Activity extends AppCompatActivity {
private ImageView iv1;
private Drawable ddd;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button2);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void show_picture_func (View v) {
iv1 = (ImageView) findViewById(R.id.imageView1);
new AsyncTask<Integer, Void, Void>(){
@Override
protected Void doInBackground(Integer... params) {
String SFTPHOST = "192.168.0.1";
int SFTPPORT = 22;
String SFTPUSER = "pi";
String SFTPPASS = "raspberry";
String SFTPWORKINGDIR = "/home/pi/imgs/";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
session.setPassword(SFTPPASS);
session.setConfig("StrictHostKeyChecking", "no");
session.setTimeout(10000);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd(SFTPWORKINGDIR);
byte[] buffer = new byte[1024];
BufferedInputStream bis = new BufferedInputStream(channelSftp.get("AAP.jpg"));
Drawable ddd = Drawable.createFromStream(bis,"ddd");
bis.close();
// bos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}.execute(1);
iv1.setImageDrawable(ddd);
//iv1.setScaleX(1);
//iv1.setScaleY(1);
Toast.makeText(getApplicationContext(), "Successful Download", Toast.LENGTH_LONG).show();
}
}
activity_main.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"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:text="Show Picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:onClick="show_picture_func"
android:id="@+id/button_show" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="?android:attr/alertDialogIcon"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/imageView1"
android:scaleX="5"
android:scaleY="5" />
</RelativeLayout>
When I press the show picture
button, the default picture of the imageview disappears, and it shows nothing. Also, this comes to logcat
:
E/HW-JPEG-DEC: [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
Note This error shows almost every time on all applications I write, but it doesn't have any effects ob them. I'm not sure about this one.
So, what is the problem? How can I solve this?
回答1:
First declare ddd
just after:
new AsyncTask<Integer, Void, Void>(){
so:
new AsyncTask<Integer, Void, Void>(){
Drawable ddd = null;
@Override
protected Void doInBackground(Integer... params) {
Then in doInBackround
change the line:
Drawable ddd = Drawable.createFromStream(bis,"ddd");
to:
ddd = Drawable.createFromStream(bis,"ddd");
and finally move iv1.setImageDrawable(ddd);
inside of onPostExecute
of your AsyncTask
(you must override it like you have done for doInBackground
):
@Override
protected void onPostExecute(Void unused) {
iv1.setImageDrawable(ddd);
}
回答2:
Use the following method to convert imageView drawable to input stream
void getInputStreamFromImageView(ImageView imageView) {
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
}
来源:https://stackoverflow.com/questions/47201504/how-to-set-an-image-in-android-app-with-sftp-input-stream