如何使用sftp输入流在Android应用程序中设置图像? [英] How to set an image in android app with sftp input stream?
问题描述
我正在使用Android Studio构建应用程序。在这个应用程序中,我想使用 Jsch sftp
从linux主机获取特定文件夹中的图像,并将此新图片设置为已存在的imageview。
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
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
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>
当我按显示图片
按钮时, imageview的默认图片消失,并且没有显示任何内容。此外,这来到 logcat
:
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?
推荐答案
首先声明 ddd
紧随其后:
new AsyncTask<Integer, Void, Void>(){
所以:
new AsyncTask<Integer, Void, Void>(){
Drawable ddd = null;
@Override
protected Void doInBackground(Integer... params) {
然后在 doInBackround
更改行:
Drawable ddd = Drawable.createFromStream(bis,"ddd");
to:
ddd = Drawable.createFromStream(bis,"ddd");
最后移动 iv1.setImageDrawable(ddd);
你的 asyncTask
的 onPostExecute
的内部(你必须像你为那样覆盖它doInBackground
):
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);
}
这篇关于如何使用sftp输入流在Android应用程序中设置图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!