简体   繁体   中英

FileNotFoundException when upload multiple images to FTP server

After selecting multiple images from a gallery, I want to upload them to an ftp server. During the upload, I get the following error:

"java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20150724_220209.jpg /storage/emulated/0/DCIM/Screenshots/Screenshot_2015-08-04-14-47-38.png "

Can anyone help?

public class MainActivity extends Activity implements View.OnClickListener{

    private LinearLayout lnrImages;
    private Button btnAddPhots;
    private Button btnSaveImages;
    private ArrayList<String> imagesPathList;
    private Bitmap yourbitmap;
    private Bitmap resized;
    private final int PICK_IMAGE_MULTIPLE =1;
    static final String FTP_HOST = "";
    static final String FTP_USER = "";
    static final String FTP_PASS = "";
    String j;
    Uri uri;
    String[] th;
    String str;
    String picturepath,currentpath;
    Button b;
    String r;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        lnrImages = (LinearLayout)findViewById(R.id.lnrImages);
        btnAddPhots = (Button)findViewById(R.id.btnAddPhots);
        btnSaveImages = (Button)findViewById(R.id.btnSaveImages);
        b=(Button)findViewById(R.id.btnSaveImages1);

        btnAddPhots.setOnClickListener(this);
        btnSaveImages.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnAddPhots:
                Intent intent = new Intent(MainActivity.this,CustomPhotoGalleryActivity.class);
                startActivityForResult(intent,PICK_IMAGE_MULTIPLE);
                break;
            case R.id.btnSaveImages:
                if(imagesPathList !=null){
                    if(imagesPathList.size()>=1) {

                        File f = new File("" + r);
                        Log.e("File", "" + f);
                        doFileUpload(f);
                        Log.d("saveimages", "" + imagesPathList);

                        Toast.makeText(MainActivity.this, imagesPathList.size() + " no of images are selected", Toast.LENGTH_SHORT).show();

                    }else{
                        Toast.makeText(MainActivity.this, imagesPathList.size() + " no of image are selected", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(MainActivity.this," no images are selected", Toast.LENGTH_SHORT).show();
                }
                break;

        }
    }

    public void doFileUpload(File f) {
        FTPClient client = new FTPClient();

        try {
            client.connect(FTP_HOST, 21);
            Log.e("clientconnect", "" + client);
            client.login(FTP_USER, FTP_PASS);
            Log.e("clientlogin", "" + client);
            client.setType(FTPClient.TYPE_BINARY);
            Log.e("clienttype", "" + client);
            client.changeDirectory("/real/");
            Log.i("", "$$$$$$$$$$$$$$$$$" + ("/real/"));
            // int reply = client.getReplyCode();

            client.upload(f, new MyTransferListener());

            // Log.e("filenameupload", "" + photoFile);
            Log.e("clientupload", "" + client);
            // Log.e("file",""+fileName);

        } catch (Exception e) {
            e.printStackTrace();
            try {
                client.disconnect(true);
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

    }

    public class MyTransferListener implements FTPDataTransferListener {

        public void started() {

            // btn.setVisibility(View.GONE);
            // Transfer started
            Toast.makeText(getApplicationContext(), " Upload Started ...",
                    Toast.LENGTH_SHORT).show();
            // System.out.println(" Upload Started ...");
        }

        public void transferred(int length) {

            // Yet other length bytes has been transferred since the last time
            // this
            // method was called
            Toast.makeText(getApplicationContext(),
                    " transferred ..." + length, Toast.LENGTH_SHORT).show();
            // System.out.println(" transferred ..." + length);
        }

        public void completed() {

            // btn.setVisibility(View.VISIBLE);
            // Transfer completed

            Toast.makeText(getApplicationContext(), " completed ...",
                    Toast.LENGTH_SHORT).show();
            // System.out.println(" completed ..." );
        }

        public void aborted() {

            // btn.setVisibility(View.VISIBLE);
            // Transfer aborted
            Toast.makeText(getApplicationContext(),
                    " transfer aborted , please try again...",
                    Toast.LENGTH_SHORT).show();
            // System.out.println(" aborted ..." );
        }

        public void failed() {

            // btn.setVisibility(View.VISIBLE);
            // Transfer failed
            System.out.println(" failed ...");
        }
        // Jibble.
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_MULTIPLE && resultCode ==Activity.RESULT_OK
                && null != data) {
            //uri=data.getData();
            // System.out.println("Current image Path is ----->" + getRealPathFromURI(uri));
                imagesPathList = new ArrayList<String>();
                String[] imagesPath = data.getStringExtra("data").split("\\|");
                try{
                    lnrImages.removeAllViews();
                }catch (Throwable e){
                    e.printStackTrace();
                }


                for (int i=0;i<imagesPath.length;i++){
                    Log.e("imagesPath can", ""+imagesPath);
                    imagesPathList.add(imagesPath[i]);
                    Log.w("imagesPathList are", ""+imagesPathList);
                    yourbitmap = BitmapFactory.decodeFile(imagesPath[i]);
                    Log.d("yourbitmap is", ""+yourbitmap);

                    ImageView imageView = new ImageView(this);
                    imageView.setImageBitmap(yourbitmap);
                    imageView.setAdjustViewBounds(true);
                    lnrImages.addView(imageView);

                    String listString = "";

                    for (String s : imagesPathList)
                    {
                        listString += s + "\t";
                    }
                    j=listString.toString();
                    uri=Uri.parse(j);
                    r=uri.toString();
                    Log.d("mnmnmnmnmnmnmhjjuigyigsuiagducfuducgfasicfgds", ""+r);
                    Log.d("anananananananananananananananananananananannananand", ""+uri);


                }

            }
        }

    private void decodeFile(String picturePath) {
        // TODO Auto-generated method stub
        BitmapFactory.Options o = new BitmapFactory.Options();
          o.inJustDecodeBounds = true;
          BitmapFactory.decodeFile(picturePath, o);

          // The new size we want to scale to
          final int REQUIRED_SIZE = 1024;

          // Find the correct scale value. It should be the power of 2.
          int width_tmp = o.outWidth, height_tmp = o.outHeight;
          int scale = 1;
          while (true) {
           if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
           width_tmp /= 2;
           height_tmp /= 2;
           scale *= 2;
          }

          // Decode with inSampleSize
          BitmapFactory.Options o2 = new BitmapFactory.Options();
          o2.inSampleSize = scale;
          yourbitmap = BitmapFactory.decodeFile(picturePath, o2);

    }}
Public class MainActivity extends Activity implements View.OnClickListener {

private LinearLayout lnrImages;
private Button btnAddPhots;
private Button btnSaveImages;
private ArrayList<String> imagesPathList;
private Bitmap yourbitmap;
private Bitmap resized;
private final int PICK_IMAGE_MULTIPLE = 1;
static final String FTP_HOST = "";
static final String FTP_USER = "";
static final String FTP_PASS = "";
String j;
Uri uri;    
String str;
String picturepath, currentpath;
Button b;
String r;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    lnrImages = (LinearLayout) findViewById(R.id.lnrImages);
    btnAddPhots = (Button) findViewById(R.id.btnAddPhots);
    btnSaveImages = (Button) findViewById(R.id.btnSaveImages);
    b = (Button) findViewById(R.id.btnSaveImages1);
    btnAddPhots.setOnClickListener(this);
    btnSaveImages.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.btnAddPhots:
        Intent intent = new Intent(MainActivity.this,
                CustomPhotoGalleryActivity.class);
        startActivityForResult(intent, PICK_IMAGE_MULTIPLE);
        break;
    case R.id.btnSaveImages:

//upload multiple images

        if (imagesPathList != null) {
            if (imagesPathList.size() >= 1) {
                for (int i = 0; i < imagesPath.length; i++) {

                    String strImg = imagesPath[i];
                    File f = new File("" + strImg);
                    Log.e("File", "" + f);
                    doFileUpload(f);
                    Log.d("saveimages", "" + imagesPathList);
                }
                Toast.makeText(
                        MainActivity.this,
                        imagesPathList.size()
                                + " no of images are selected",
                        Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(
                        MainActivity.this,
                        imagesPathList.size() + " no of image are selected",
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(MainActivity.this, " no images are selected",
                    Toast.LENGTH_SHORT).show();
        }
        break;

}
}
public void doFileUpload(File f) {
    FTPClient client = new FTPClient();

    try {
        client.connect(FTP_HOST, 21);
        Log.e("clientconnect", "" + client);
        client.login(FTP_USER, FTP_PASS);
        Log.e("clientlogin", "" + client);
        client.setType(FTPClient.TYPE_BINARY);
        Log.e("clienttype", "" + client);
        client.changeDirectory("/ramesh2/");
        Log.i("", "$$$$$$$$$$$$$$$$$" + ("/ramesh2/"));
        // int reply = client.getReplyCode();

        client.upload(f, new MyTransferListener());

        // Log.e("filenameupload", "" + photoFile);
        Log.e("clientupload", "" + client);
        // Log.e("file",""+fileName);

    } catch (Exception e) {
        e.printStackTrace();
        try {
            client.disconnect(true);
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }

}
public class MyTransferListener implements FTPDataTransferListener {

    public void started() {

        // btn.setVisibility(View.GONE);
        // Transfer started
        Toast.makeText(getApplicationContext(), " Upload Started ...",
                Toast.LENGTH_SHORT).show();
        // System.out.println(" Upload Started ...");
    }

    public void transferred(int length) {

        // Yet other length bytes has been transferred since the last time
        // this
        // method was called
        Toast.makeText(getApplicationContext(),
                " transferred ..." + length, Toast.LENGTH_SHORT).show();
        // System.out.println(" transferred ..." + length);
    }

    public void completed() {

        // btn.setVisibility(View.VISIBLE);
        // Transfer completed

        Toast.makeText(getApplicationContext(), " completed ...",
                Toast.LENGTH_SHORT).show();
        // System.out.println(" completed ..." );
    }

    public void aborted() {

        // btn.setVisibility(View.VISIBLE);
        // Transfer aborted
        Toast.makeText(getApplicationContext(),
                " transfer aborted , please try again...",
                Toast.LENGTH_SHORT).show();
        // System.out.println(" aborted ..." );
    }

    public void failed() {

        // btn.setVisibility(View.VISIBLE);
        // Transfer failed
        System.out.println(" failed ...");
    }
    // Jibble.
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE_MULTIPLE
            && resultCode == Activity.RESULT_OK && null != data) {
        // uri=data.getData();
        // System.out.println("Current image Path is ----->" +
        // getRealPathFromURI(uri));
        imagesPathList = new ArrayList<String>();
         imagesPath = data.getStringExtra("data").split("\\|");
        try {
            lnrImages.removeAllViews();
        } catch (Throwable e) {
            e.printStackTrace();
        }

        for (int i = 0; i < imagesPath.length; i++) {
            Log.e("imagesPath can", "" + imagesPath);
            imagesPathList.add(imagesPath[i]);
            Log.w("imagesPathList are", "" + imagesPathList);
            yourbitmap = BitmapFactory.decodeFile(imagesPath[i]);
            Log.d("yourbitmap is", "" + yourbitmap);

            ImageView imageView = new ImageView(this);
            imageView.setImageBitmap(yourbitmap);
            imageView.setAdjustViewBounds(true);
            lnrImages.addView(imageView);

            String listString = "";

            for (String s : imagesPathList) {
                listString += s + "\t";
            }
            j = listString.toString();
            uri = Uri.parse(j);
            r = uri.toString();
            Log.d("mnmnmnmnmnmnmhjjuigyigsuiagducfuducgfasicfgds", "" + r);
            Log.d("anananananananananananananananananananananannananand",
                    "" + uri);

} }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM