user3032387 user3032387 - 1 month ago 21
Android Question

Android: Upload image just taken bij camera

I'm trying to make an app that uses the camera to take photos and sends this to my NAS, I managed to do create an app through a tutorial, which takes a picture, saves the picture and shows it onscreen. Another tutorial shows me how to upload a "selected" image by pressing an "extra" button on the screen, it's just that I want to get rid of this step and instead of showing the picture, it directly sends the picture to my NAS.

The current app I created:

package com.deshpande.camerademo;

public class MainActivity extends AppCompatActivity {

private Button takePictureButton;
private ImageView imageView;
private Uri file;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

takePictureButton = (Button) findViewById(R.id.button_image);
imageView = (ImageView) findViewById(R.id.imageview);

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
takePictureButton.setEnabled(false);
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == 0) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
takePictureButton.setEnabled(true);
}
}
}

public void takePicture(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = Uri.fromFile(getOutputMediaFile());
intent.putExtra(MediaStore.EXTRA_OUTPUT, file);

startActivityForResult(intent, 100);
}

private static File getOutputMediaFile(){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "WedCam");

if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()){
Log.d("WedCam", "failed to create directory");
return null;
}
}

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
if (resultCode == RESULT_OK) {

try {

final FTPClient client = new FTPClient();
final File image = new File(file.getPath());
final FileInputStream fis = new FileInputStream(image);

Thread thread = new Thread(new Runnable() {

@Override
public void run() {
try {

client.connect("ftp://127.0.0.1:2221");
client.login("francis", "francis");

client.storeFile(image.getName(), fis);
client.logout();

} catch (Exception e) {
e.printStackTrace();
}
}
});

thread.start();

} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}


Thanks to @Matthias F. I got it all working, this guy is great!

Answer

You need to use your saved URI from the camera for the FileInputStream. Try it with this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 100) {
        if (resultCode == RESULT_OK) {

            try {

                final FTPClient client = new FTPClient();
                final File image = new File(file.getPath());
                final FileInputStream fis = new FileInputStream(image);

                Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try  {

                            client.connect("127.0.0.1", 2221);
                            client.login("francis", "francis");
                            client.setFileType(FTP.BINARY_FILE_TYPE);

                            client.storeFile(image.getName(), fis);
                            client.logout();

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

                thread.start(); 

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Now the FileInputStream points to the created image.

EDIT: The problem was that the upload was executed on the ui thread and that is not allowed in android. Now a new thead is created for the upload process. Also I forgot to say that you need to access a android permission for using network functionalities. So you need to set <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> in the AndroidManifest.xml.