Android:上传图片刚拍摄的bij相机

时间:2016-10-25 14:11:49

标签: android image upload camera photo

我试图制作一个使用相机拍照并将其发送到我的NAS的应用程序,我设法通过教程创建应用程序,该教程拍照,保存图片并在屏幕上显示。另一个教程向我展示了如何上传一个"选择"通过按"额外"屏幕上的按钮,只是我想摆脱这一步,而不是显示图片,它直接将图片发送到我的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.我一切顺利,这家伙很棒!

1 个答案:

答案 0 :(得分:0)

您需要使用摄像头保存的URI作为FileInputStream。试试吧:

GET

现在DELETE指向创建的图像。

编辑: 问题是上传是在ui线程上执行的,而android中是不允许的。现在为上传过程创建了一个新的thead。此外,我忘了说你需要访问Android权限使用网络功能。 So you need to set @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