当前位置: 首页 > 知识库问答 >
问题:

读取文件时出错:java。伊奥。FileNotFoundException:打开失败:eNote(没有这样的文件或目录)

葛驰
2023-03-14

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

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中选择一张图片并上传到b()start。。但它在运行时会返回以下错误代码(应用程序不会崩溃):

D/log: Uri is:content://com.android.providers.media.documents/document/image: 30947

日志:上传开始了!

E/错误

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

共有1个答案

潘凯
2023-03-14
in = new FileInputStream(filePath.toString());

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

in = getContentResolver().openInputStream(filePath);

正如您在本代码后面所说的。

 类似资料: