读取文件时出错:java.io.FileNotFoundException:open失败:enoint(没有这样的文件或目录)

读取文件时出错:java.io.FileNotFoundException:open失败:enoint(没有这样的文件或目录),java,android,image,android-studio,baasbox,Java,Android,Image,Android Studio,Baasbox,我需要从我的图库中选择一个图像,然后放在我的baasbox服务器上。 为此,我需要图像的输入流 BaasBox文档报告如下: InputStream data = ...; // input stream to upload BaasFile file = new BaasFile(); file.upload(data, new BaasHandler<BaasFile>() { @Override public void ha

我需要从我的图库中选择一个图像,然后放在我的baasbox服务器上。 为此,我需要图像的输入流

BaasBox文档报告如下:

 InputStream data = ...; // input stream to upload
 BaasFile file = new BaasFile();
 file.upload(data, new BaasHandler<BaasFile>() {
            @Override
            public void handle(BaasResult<BaasFile> baasResult) {
                if( baasResult.isSuccess() ) {
                    Log.d("LOG","File uploaded with permissions");
                } else {
                    Log.e("LOG","Deal with error",baasResult.error());
                }
            }
        });
所以,我在网上搜索怎么能做到这一点

我的主要活动是: 所以,当它启动时,我从gallery中选择一张图片并上传到B start。。但它在运行时返回以下错误代码,即应用程序未崩溃:

D/log:Uri是:content://com.android.providers.media.documents/document/image%3A30947

日志:上传开始了

读取文件时出错:java.io.FileNotFoundException:content:/com.android.providers.media.documents/document/image%3A30947:打开失败:没有这样的文件或目录

有必要将InputStream放入baasBox上载方法中。 你能帮助我吗? 谢谢

这不是在Uri上获取InputStream的方式。使用:


正如您在后面的代码中所做的。

您确实有一个名为com.android.providers.media.documents/document/image%3A30947的文件
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private int PICK_IMAGE_REQUEST = 1;

private Button buttonChoose;
private Button buttonUpload;
private Button buttonView;
private ImageView imageView;

private Bitmap bitmap;

private Uri filePath;

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

    buttonChoose = (Button) findViewById(R.id.buttonChoose);
    buttonUpload = (Button) findViewById(R.id.buttonUpload);
    buttonView = (Button) findViewById(R.id.buttonViewImage);

    imageView = (ImageView) findViewById(R.id.imageView);

    buttonChoose.setOnClickListener(this);
    buttonUpload.setOnClickListener(this);

    //omitted information about baasbox init and login with a admin user


private void showFileChooser() { //dal tutorial
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        filePath = data.getData();
        Log.d("log","Uri is: " + filePath.toString());
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);
            Log.d("log","upload started!");
            uploadToBB(filePath);

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

public void uploadToBB(Uri filePath){
    FileInputStream in;
    BufferedInputStream buf;
    try {
        in = new FileInputStream(filePath.toString());
        buf = new BufferedInputStream(in);
        Bitmap bMap = BitmapFactory.decodeStream(buf);
        InputStream data = getContentResolver().openInputStream(filePath);// input stream to upload
        BaasFile file = new BaasFile();
        file.upload(data, new BaasHandler<BaasFile>() {
            @Override
            public void handle(BaasResult<BaasFile> baasResult) {
                if( baasResult.isSuccess() ) {
                    Log.d("LOG","File uploaded with permissions");
                } else {
                    Log.e("LOG","Deal with error",baasResult.error());
                }
            }
        });

        if (in != null) {
            in.close();
        }
        if (buf != null) {
            buf.close();
        }
    } catch (Exception e) {
        Log.e("Error reading file", e.toString());
    }
}
in = new FileInputStream(filePath.toString());
in = getContentResolver().openInputStream(filePath);