Android: 上传刚用相机拍摄的图像

Android: Upload image just taken bij camera

我正在尝试制作一个使用相机拍照并将其发送到我的 NAS 的应用程序,我设法通过教程创建了一个应用程序,该应用程序拍摄照片、保存照片并将其显示在屏幕上.另一个教程告诉我如何通过按屏幕上的 "extra" 按钮上传 "selected" 图片,只是我想去掉这一步而不是显示图片,而是直接发送图片到我的 NAS。

我当前创建的应用:

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

感谢@Matthias F。我一切正常,这家伙太棒了!

您需要将您从相机保存的 URI 用于 FileInputStream。试试这个:

@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();
            }
        }
    }
}

现在 FileInputStream 指向创建的图像。

编辑: 问题是上传是在 ui 线程上执行的,这在 android 中是不允许的。现在为上传过程创建了一个新的 thead。另外我忘了说您需要访问 android 权限才能使用网络功能。 So you need to set <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />AndroidManifest.xml.