I am trying to upload files(pdf,ppt,docx) from local file using new firebase storage. I tried code from https://firebase.google.com/docs/storage/android/upload-files#upload_from_a_local_file , but got error.
Found answer.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("*/*");
startActivityForResult(intent, RESULT_LOAD_IMAGE);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null){
Uri selectedImage = data.getData();
final StorageReference photoRef = storageRef.child(selectedImage.getLastPathSegment());
photoRef.putFile(selectedImage);
}
}
Related
I am using below code for choose pdf from Storage in Android 10.
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select Pdf"), PDF_REQ_CODE);
In startActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PDF_REQ_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
uri = data.getData();
}
}
Uri returns like content://com.android.providers.media.documents/document/document:6865
But I want absolute path like storage/..../sample.pdf
Help me to set the uri of the image button to the image that i select form the internal storage. I have already given required permissions in the android_manifest.xml file. The problem is that when i browse to the internal storage of phone the images are unselectable.
public void onUploadImageClick(View view){
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("Image/*");
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageURI(uri);
}
}
public void onUploadImageClick(View view){
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
Then in your on activity result method keep this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageURI(uri);
}
}
EDIT
yes the problem is I forgot to add .Media
so use this line of code:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
You can try below code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
String pathName = uri.toString();
Bitmap bmp = BitmapFactory.decodeFile(pathName);
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageBitmap(bmp);
}
}
Can you try this. You could replace ImageView to ImageButton
String qrPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Image.png";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap mustOpen = BitmapFactory.decodeFile(qrPath, options);
setContentView(R.layout.qr_image);
ImageView imageView = (ImageView) findViewById(R.id.qr_image);
imageView.setImageBitmap(mustOpen);
I use this to let the user pick an image from gallery:
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/*");
i.putExtra("crop", "true");
i.putExtra("scale", true);
i.putExtra("return-data", true);
startActivityForResult(i, RESULT_LOAD_IMAGE);
Then it will be displayed in an ImageView:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
final Bundle extras = data.getExtras();
Bitmap photo = extras.getParcelable("data");
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(photo);
}
}
It works but the picture now has a bad quality.
How to fix this?
You shouldn't specify any of those extras because they are device-dependent. Just do this:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
and
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK)
{
Uri selectedImage = data.getData();
//load Uri into ImageView
}
}
I would look into using a library like Picasso to load the images instead of just doing imageView.setImageBitmap(photo) if your app is image intensive.
Does Android have a way to browse and pick any file from an SD card using intents?
Something like:
String uri = (Environment.getExternalStorageDirectory()).getAbsolutePath();
Intent i = new Intent(Intent.ACTION_PICK, Uri.parse(uri));
I am trying to send a file to other devices using Bluetooth. I can send if I give the full path of the file name in my code. I want my users to pick the file that should be send.
You can use the following code:
Intent mediaIntent = new Intent(Intent.ACTION_GET_CONTENT);
mediaIntent.setType("video/*"); // Set MIME type as per requirement
startActivityForResult(mediaIntent,REQUESTCODE_PICK_VIDEO);
Then you can get the path in onActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUESTCODE_PICK_VIDEO
&& resultCode == Activity.RESULT_OK) {
Uri videoUri = data.getData();
Log.d("", "Video URI= " + videoUri);
}
}
For general browsing use this (e.g. Music file):
Intent intent = new Intent();
intent.setType("*/*");
if (Build.VERSION.SDK_INT < 19) {
intent.setAction(Intent.ACTION_GET_CONTENT);
intent = Intent.createChooser(intent, "Select file");
} else {
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
String[] mimetypes = { "audio/*", "video/*" };
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
}
startActivityForResult(intent, Constants.REQUEST_BROWSE);
And receive the browsed data here:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.REQUEST_BROWSE
&& resultCode == Activity.RESULT_OK && data != null) {
Uri uri = data.getData();
if (uri != null) {
// TODO: handle your case
}
}
}
How do I read and write image to/from a local file in android?
I want to take an image from a file, save it in memory and then read it from file.
I am using this code to write image in file:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String fileName=GetFileName().replace('.', ' ')+".png";
File out = new File(Environment.getExternalStorageDirectory()+File.separator +fileName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(intent, REQUEST_IMAGE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
wb.loadUrl(sendUrl);
if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
String imgPath= data.getExtras().get("Uri").toString();
bm = BitmapFactory .decodeFile(imgPath);
}
}
How can I read the image back?
Try this---
Uri mUriSavedImage;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String fileName=GetFileName().replace('.', ' ')+".png";
File out = new File(Environment.getExternalStorageDirectory()+File.separator +fileName);
mUriSavedImage= Uri.fromFile(out);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mUriSavedImage);
startActivityForResult(intent, REQUEST_IMAGE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
wb.loadUrl(sendUrl);
if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
String imgPath= mUriSavedImage.getPath();
bm = BitmapFactory.decodeFile(imgPath);
}
}