Get file size in activity result in android - java

I have this code :
public static final int RESULT_OK = -1;
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri fileUri = data.getData();
File F = new File(fileUri.getPath());
Log.d("File", "File Uri: " + fileUri.toString());
Intent intent = new Intent(getContext(),activity_file_sharing.class);
intent.putExtra("Filepath", fileUri.toString());
startActivity(intent);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
F.length() always return 0 i need to path the size of the selected file to the other activity
can anyone help

Please, check this link:
how do I get file size of temp file in android?
Try this:
File file = new File(selectedPath);
int file_size = Integer.parseInt(String.valueOf(file.length()/1024));

Related

sending Uri from activity to another

I have this code :
Activity 1
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri fileUri = data.getData();
Log.d("File", "File Uri: " + fileUri.toString());
Intent intent = new Intent(getContext(),activity_file_sharing.class);
intent.putExtra("Filepath", fileUri);
startActivity(intent);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
In activity_file_sharing
Bundle extras = getIntent().getExtras();
if (extras != null && extras.containsKey("Filepath")) {
FPath = Uri.parse(getIntent().getStringExtra("Filepath").toString());
}
FPath = Uri.parse(getIntent().getStringExtra("Filepath").toString());
I am always getting a null reference on the line above , i can't figure out what is the problem
I hope you guys can help
Save it as a String:
Intent intent = new Intent(getContext(),activity_file_sharing.class);
intent.putExtra("Filepath", fileUri.toString());
startActivity(intent);

Saving photo in my application

I'm trying to save a photo that I have already captured in my application (to use it later like as an icon for my contact) but I don't know if I must change it from uri to bitmap or any other type. I would like to save it as a .png like my others photos
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, 1020);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1020 && resultCode == RESULT_OK) {
mImageUri = data.getData();
Uri mImageUri2 = mImageUri;
Uri aux1=mImageUri;
Bitmap foto = photoUtils.getImage(aux1);
Drawable fotofinal =new BitmapDrawable(getResources(),foto);
Thanks for the help.

aFileChoser in fragmentActivity

i made this example: swipe tab
but i can't make the aFileChooser to work with fragmanactivity.
I added it as it has to, but cant get the result_ok int the onActivityresult.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CHOOSER:
if (resultCode == RESULT_OK) {
final Uri uri = data.getData();
File file = FileUtils.getFile(uri);
Log.e("", file.getAbsolutePath());
}
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
How can i manage to get the RESUL_OK to work,because the eclipse said:
RESULT_OK cannot be resolved to a variable
replace RESULT_OK with Activity.RESULT_OK then it will work..

getting uri from android camera on all versions

I'm trying to take a picture with my app, save it and retrieve the url. Currently I have:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case TAKE_PICTURE:
{
if (resultCode == RESULT_OK)
{
File file = new File(getPath(data.getData()));
Date dt = new Date(file.lastModified());
String datum = dt.toLocaleString();
Comment_Resource photo = new Comment_Resource(file.getAbsolutePath(), datum, true);
}
break;
}
}
}
this works on some phones but a lot of phones don't save the picture causing a nullpointer.
try this:
Uri uri = Uri.fromFile(file);

Using the camera intent to take a picture without an sdcard

I'm having trouble using the camera when there's no sdcard present.
When there is an sdcard, using the camera is trivial, e.g.
http://www.vogella.com/articles/AndroidCamera/article.html + a plethora of other examples.
However, I need to make my app available to devices which don't have SD cards, (e.g. the Sony Xperia series.) I've tried modifying the code such that I'm using the internal storage (I think):
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(getDir("myDirec", Context.MODE_WORLD_WRITEABLE), "tmp_photo_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
file.createNewFile();
mImageCaptureUri = Uri.fromFile(file);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
intent.putExtra("return-data", true);
However, upon result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intentReturn) {
if (resultCode != RESULT_OK)
return;
String path = mImageCaptureUri.getPath();
Bitmap bitmap = BitmapFactory.decodeFile(path);
...
bitmap is null.
Which leads me to believe that there's some permissions issue....maybe?
I've tried some of the other internal storage options, http://developer.android.com/guide/topics/data/data-storage.html#filesInternal e.g. getFilesDir() but the same result: null bitmap.
Has anyone had any success in using the camera without an sdcard?
Try this. It works..
private Uri imageUri;
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btnImageCapture:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File dir = context.getDir("directory", Context.MODE_PRIVATE);
File photo = new File(dir, "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, OPEN_CAMERA);
break;
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case OPEN_CAMERA:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ImageView imageView = (ImageView) findViewById(R.id.ImageView);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
imageView.setImageBitmap(bitmap);
Toast.makeText(this, selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
}
}
}
}

Categories

Resources