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

时间:2016-01-14 00:57:38

标签: java android image android-studio baasbox

我需要从我的画廊中挑选一张图片并放在我的baasbox服务器上。 为此,我需要一个我的图像的InputStream。

BaasBox Docs报告:

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

所以,我在网上搜索了怎么做。

我的主要活动是:

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

所以,当它开始时,我选择了一个来自gallery的图片并且uploadToBB()开始..但它在RunTime返回这个错误代码(应用程序没有崩溃):

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

D / log:已开始上传!

E /错误读取文件:java.io.FileNotFoundException:content:/com.android.providers.media.documents/document/image%3A30947:open failed:ENOENT(没有这样的文件或目录)

必须在一个baasBox upload()方法中放入一个InputStream。 你能帮助我吗? 谢谢!

1 个答案:

答案 0 :(得分:3)

in = new FileInputStream(filePath.toString());

这不是InputStream Uri的{​​{1}}。使用:

in = getContentResolver().openInputStream(filePath);

正如您稍后在此代码中所做的那样。