0

My Objective is:

I should show image path in imageview when its converted into file object.

I am trying with some solutions with different file object are:

File f=null;
    URL url = new URL(info4Model.getExternal_photos().get(position).getImage_path());
        try {
         //   f = new File(url.toURI());//java.lang.IllegalArgumentException: Expected file scheme in URI:
            //f = new File(url.getPath());//  not showing file object in imageview
            //f = new File(url.getFile());// not showing file object in imageview
            f = getFileFromBitmap(info4Model.getExternal_photos().get(position).getImage_path());
             //android.os.NetworkOnMainThreadException , AsyncTask option left only?

        } catch (Exception e) {


        }
///////////////////////////////////
public static Bitmap getBitmapFromURL(String url) {
    try {
        URL url1 = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap bitmapFrmUrl = BitmapFactory.decodeStream(input);
        return bitmapFrmUrl;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }catch (Exception e){
        e.printStackTrace();
        return null;
    }
}
File getFileFromBitmap(String url) {
    Bitmap bmp = getBitmapFromURL(url);

    SimpleDateFormat simpleDateFormat =
            new SimpleDateFormat("MMddhhmmss");
    String dateAsString = simpleDateFormat.format(new Date());
    File filesDir = getActivity().getFilesDir();
    File imageFile = new File(filesDir, dateAsString + ".png");

    OutputStream os;
    try {
        os = new FileOutputStream(imageFile);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
        os.flush();
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return imageFile;
}

Here all the file objects are showing failure results in comments after the statement.

2 Answers 2

1

If u get the image url..from api...

Then, Uae picasso for loading ur image into imageview like.. Picasso.with(acticityname.this)

   .load("YOUR IMAGE URL HERE")

   .into(imageView);

And

For api to load your image path.. You have to use multipart..

For that..

File file= new File(ur image path); FileBody a1=new FileBody(file);

MultipartEntity mpEntity = new MultipartEntity(); mpEntity.addPart("param name", a1);

Hope,you may get help with this.

Sign up to request clarification or add additional context in comments.

Comments

0
public class FileModel {
    File Url;
    File path;
    File file;
    File bitMap;

    public File getUrl() {
        return Url;
    }

    public void setUrl(File url) {
        Url = url;
    }

    public File getPath() {
        return path;
    }

    public void setPath(File path) {
        this.path = path;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public File getBitMap() {
        return bitMap;
    }

    public void setBitMap(File bitMap) {
        this.bitMap = bitMap;
    }
    }  

  ArrayList<FileModel> fileArrayList = new ArrayList<>();

FileModel fileModel = new FileModel();
        File f=null;
        URL url = new URL(info4Model.getExternal_photos().get(position).getImage_path());
        try {
            f = new File(url.toURI());//java.lang.IllegalArgumentException: Expected file scheme in URI:
            fileModel.setUrl(f);
            f = new File(url.getPath());//  not showing file object in imageview
            fileModel.setPath(f);
            f = new File(url.getFile());// not showing file object in imageview
            fileModel.setFile(f);
            f = getFileFromBitmap(info4Model.getExternal_photos().get(position).getImage_path());
            fileModel.setBitMap(f);
            //android.os.NetworkOnMainThreadException , AsyncTask option left only?

        } catch (Exception e) {

            e.printStackTrace();
        }

        fileArrayList.add(fileModel);

finally File arraylist is ready to use.

I hope it's helpful for you ,,,!

2 Comments

Thanks, but all the objects have commented results as you can see. So My question to get from all these no one single File object is working.
you need to crate a one model class then put all the values in model and and model in ArrayList ...!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.