Skip to content Skip to sidebar Skip to footer

How To Set An Image In Android App With Sftp Input Stream?

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

Solution 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);
}

Solution 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);
}

Post a Comment for "How To Set An Image In Android App With Sftp Input Stream?"