Android捕获图像并获取实际路径

时间:2015-07-24 11:22:33

标签: android ftp camera android-camera

我想创建一个拍摄照片/视频并将其发送到我的FTP服务器的应用程序。

这是我的onActivityResult代码;

@Override
protected void onActivityResult(int requestCode, int resultCode,final Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PHOTO_CAPTURE && resultCode == RESULT_OK) {
        // Upload sdcard file
        new AsyncTask(){
            @Override
            protected Object doInBackground(Object[] params) {
                File f = new File(data.getData() + "");
                try {
                    uploadFile(f);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute();
        // Toast.makeText(this, "Photo saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
    }

    if (requestCode == VIDEO_CAPTURE && resultCode == RESULT_OK) {
        // Upload sdcard file
        new AsyncTask(){
            @Override
            protected Object doInBackground(Object[] params) {
                File f = new File(data.getData() + "");
                try {
                    uploadFile(f);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute();
        // Toast.makeText(this, "Video saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
    }
}

错误说没有这样的文件或目录。如何获取视频或图像的实际路径?

感谢您的时间。

编辑 - 编辑 - 编辑

我使用了另一种方法来获取路径

 public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

现在我没有收到任何错误,但是当我打开我的ftp文件夹时,我看不到上传的图片:S

这是我的uploadFile方法;

 public void uploadFile(File fileName) throws IOException {
    SimpleFTP ftp = new SimpleFTP();
    // Connect to an FTP server on port 21.
    ftp.connect(server, port, user, pass);
    // Set binary mode.
    ftp.bin();
    // Change to a new working directory on the FTP server.
    ftp.cwd("upload");
    // Upload some files.
    ftp.stor(fileName.getAbsoluteFile());
    // Quit from the FTP server.
    ftp.disconnect();
}

2 个答案:

答案 0 :(得分:0)

从您的活动中更改此代码,请参阅以下内容:

if (requestCode == VIDEO_CAPTURE && resultCode == RESULT_OK) {
    // Upload sdcard file
    new AsyncTask(){
        @Override
        protected Object doInBackground(Object[] params) {

            try {

                //Create a empty file what you want...
                File f = new File("Write here your path what you want");

                //Open OutputStream
                FileOutputStream fos = new FileOutputStream(f);
                //Write file
                fos.write(data);
                //File is created in your path

                uploadFile(f);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }.execute();
    // Toast.makeText(this, "Video saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
}

告诉我,如果我帮助你进行这些改变和良好的编程!

答案 1 :(得分:0)

你必须使用File对象的getAbsolutePath()它返回文件的路径。