I need to pick a image from my gallery and put on my baasbox server. To do this,I need a InputStream of my image.
BaasBox Docs report this:
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());
}
}
});
So,I searched in internet how can do this.
My MainActivity is this:
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());
}
}
So,when it started,I choose a picture from gallery and uploadToBB() start.. but it return me this error code at RunTime (the app not crash):
D/log: Uri is:content://com.android.providers.media.documents/document/image%3A30947
D/log: upload started!
E/Error reading file: java.io.FileNotFoundException: content:/com.android.providers.media.documents/document/image%3A30947: open failed: ENOENT (No such file or directory)
Is necessary put a InputStream in a baasBox upload() method. Can you help me? Thanks!