I need to choose a pdf from sdcard and convert it to byte array. I don't want to show it . I searched a lot but there was no answer to this question.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_MAGAZINE_FILE && resultCode == RESULT_OK && data != null) {
// Let's read picked image data - its URI
Uri uri = data.getData();
System.out.println(uri);
System.out.println(uri.getPath());
File file = new File(uri.getPath());
//init array with file length
byte[] bytesArray = new byte[(int) file.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
fis.read(bytesArray); //read file into bytes[]
fis.close();
System.out.println(bytesArray);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
and I got this error :
java.io.FileNotFoundException: /document/primary:myfile.pdf: open failed: ENOENT (No such file or directory)
At best, your code will only work if the Uri being delivered to onActivityResult() happens to have a file scheme. Yours does not. It has a content scheme. Your code will also fail a lot due to an OutOfMemoryError, because your code fails to allocate the byte[]. PDF files can be rather large.
So, your first task is to find some other solution instead of reading the entire PDF file into a byte[], as this will be unreliable, and there is nothing that you can do to fix that, other than by not doing it.
Eventually, to get an InputStream on the content identified by the Uri, use a ContentResolver and openInputStream().
And, in the long term, you need to move this I/O to a background thread, as right now you will freeze your UI for the time that it takes you to read in the data.
Note that the use of ContentResolver for Uri values, and the use of threads, is covered in any decent book or course on Android app development.
Thanks https://stackoverflow.com/users/115145/commonsware for your help.
I changed my code to this and it works. It is better to do it with AsyncTask.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_MAGAZINE_FILE && resultCode == RESULT_OK && data != null) {
// Let's read picked image data - its URI
Uri uri = data.getData();
File file = new File(uri.getPath());
try {
InputStream is = getActivity().getContentResolver().openInputStream(uri);
byte[] bytesArray = new byte[is.available()];
is.read(bytesArray);
//write to sdcard
/*
File myPdf=new File(Environment.getExternalStorageDirectory(), "myPdf.pdf");
FileOutputStream fos=new FileOutputStream(myPdf.getPath());
fos.write(bytesArray);
fos.close();*/
System.out.println(fileString);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
Log.d("++++++++++", "++++ data log +++" + imageBitmap);
}
}
I am having two different classes. One class is for adding image and uploading it to server and another class for editing the saved image with new image. So for getting image from sdcard, in add class I am using startactivityforresult and onactivityresult. Same thing I am trying to use in edit class because in this class im having an ImageView where new image is added but it is not allowing me to create onactivityresult in edit class. Saying error as onactivityresult is already defined. If someone knows please help me.
code:
#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) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
imageToUpload.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
Use it like this:
startActivityForResult (intent, int requestCode);
Then in onActivityResult you can use switch case using requestCode.
Like:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode) {
//Have your cases here
}
}
I have been trying to create an android app with the integration of Adobe creative SDJ but I have encountered some problems.
I have created an Activity where one launches camera or gallery to select an image for editing it. The camera launches and the photo is captured and saved in this path:
storage/emulated/0/Pictures/myAppName/myImage.jpg
It is also supposed to be displayed in an ImageView in the same activity but it isn't being displayed. In another activity, I have integrated the ImageEditing UI of the creative sdk from Adobe which needs an imageUri as an input image.
Here are my codes for displaying Image in the ImageView:
#Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// camera codes
String path = "storage/emulated/0/Pictures/Touch/touch.jpg";
resultImageView.setImageDrawable(Drawable.createFromPath(path));
// gallery codes
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
imageUri = data.getData();
resultImageView.setImageURI(imageUri);
}
}
How can I convert the ImageView into Uri and send it to the next activity? Thanks in advance!
I think you are doing wrong for camera code. as you say your gallery code is working fine .try this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try{
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 0) {
//taking clicked image from Intent of activity
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");//storing image
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//adding clicked image to ImageView
imgClicked.setImageBitmap(thumbnail);
Hope this will help you..:-)
I'm using the class FileUtils and library ipaulpro.afilechooser.
I fixed my code but I get "copyFile" underlined in red. Will you help me understand why? Thanks
private void import(File from){
File to = new File("/data/data/"+getPackageName()+"/databases/cio.db");
try {
FileUtils.copyFile(from, to);
Toast.makeText(this, getString(R.string.Toast_o) , Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, getString(R.string.Toast_to) , Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == this.FILE_CHOOSE){
if (resultCode == RESULT_OK){
// The URI of the selected file
Uri uri = data.getData();
// Create a File from this Uri
File file = com.ipaulpro.afilechooser.utils.FileUtils.getFile(uri);
importDB(file);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Because there is no method called copyFile in https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
FileUtils of ipaulpro.afilechooser.
The copyFile method is available in the android.os.FileUtil class, but it looks like you are using ipaulpro.afilechooser.
If you want to use the android.os change your code to
android.os.FileUtils.copyFile(from, to);
Although personally I would find it a bit confusing and maybe unnecessary to use both versions of this class.
I have an app published and one of the fundamental features is to allow the user to take a picture, and then save that photo in a specific folder on their External Storage.
Everything seems to be working fine, but I've gotten two reports now that claim that after taking a photo, and clicking "Done" to exit the camera (and return back to the Activity), the app is Forced Closed, bringing the user back to the home screen.
This happens on a Samsung Nexus S and the Galaxy Tab. Below I've posted my code to show I set up my intent and how I handle saving and displaying the photo in onActivityResult(). Any guidance on what might be causing it to crash after they click "Done" to exit the camera app, would be greatly appreciated!
Again, this seems to be working fine on most devices but I was wondering if their is a more efficient, universal approach I should be taking. Thank you
How I'm firing the Camera Intent
case ACTION_BAR_CAMERA:
// numbered image name
fileName = "image_" + String.valueOf(numImages) + ".jpg";
output = new File(direct + File.separator + fileName); // create
// output
while (output.exists()) { // while the file exists
numImages++; // increment number of images
fileName = "image_" + String.valueOf(numImages) + ".jpg";
output = new File(outputFolder, fileName);
}
camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
uriSavedImage = Uri.fromFile(output); // get Uri of the output
camera.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); //pass in Uri to camera intent
startActivityForResult(camera, 1);
break;
default:
return super.onHandleActionBarItemClick(item, position);
}
return true;
}
How I'm setting up onActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) { // If data was passed successfully
Bundle extras = data.getExtras();
//Bundle extras = data.getBundleExtra(MediaStore.EXTRA_OUTPUT);
/*ad = new AlertDialog.Builder(this).create();
ad.setIcon(android.R.drawable.ic_menu_camera);
ad.setTitle("Save Image");
ad.setMessage("Save This Image To Album?");
ad.setButton("Ok", this);
ad.show();*/
bmp = (Bitmap) extras.get("data"); // Set the bitmap to the bundle
// of data that was just
// received
image.setImageBitmap(bmp); // Set imageview to image that was
// captured
image.setScaleType(ScaleType.FIT_XY);
}
}
First lets make it clear - we have two options to take image data in onActivityResult from Camera:
1. Start Camera by passing the exact location Uri of image where you want to save.
2. Just Start Camera don't pass any Loaction Uri.
1 . IN FIRST CASE :
Start Camera by passing image Uri where you want to save:
String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/picture.jpg";
File imageFile = new File(imageFilePath);
Uri imageFileUri = Uri.fromFile(imageFile); // convert path to Uri
Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
it.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(it, CAMERA_RESULT);
In onActivityResult receive the image as:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (RESULT_OK == resultCode) {
iv = (ImageView) findViewById(R.id.ReturnedImageView);
// Decode it for real
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = false;
//imageFilePath image path which you pass with intent
Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
// Display it
iv.setImageBitmap(bmp);
}
}
2 . IN SECOND CASE:
Start Camera without passing image Uri, if you want to receive image in Intent(data) :
Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(it, CAMERA_RESULT);
In onActivityResult recive image as:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (RESULT_OK == resultCode) {
// Get Extra from the intent
Bundle extras = data.getExtras();
// Get the returned image from extra
Bitmap bmp = (Bitmap) extras.get("data");
iv = (ImageView) findViewById(R.id.ReturnedImageView);
iv.setImageBitmap(bmp);
}
}
*****Happy Coding!!!!*****
On the camera button click event you can try this:
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(getTempFile(this)));
startActivityForResult(intent, TAKE_PHOTO_CODE);
declare TAKE_PHOTO_CODE globally as:
private static final int TAKE_PHOTO_CODE = 1;
Add getTempFile Function in the code which will help to save the image named myImage.png you clicked in the sdcard under the folder named as your app's package name.
private File getTempFile(Context context) {
final File path = new File(Environment.getExternalStorageDirectory(),
context.getPackageName());
if (!path.exists()) {
path.mkdir();
}
return new File(path, "myImage.png");
}
Now on OnActivityResult function add this:
if (requestCode == TAKE_PHOTO_CODE) {
final File file = getTempFile(this);
try {
Uri uri = Uri.fromFile(file);
Bitmap captureBmp = Media.getBitmap(getContentResolver(),
uri);
image.setImageBitmap(captureBmp);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
in case you get Memory issues than add below code:
#Override
protected void onPause() {
image.setImageURI(null);
super.onPause();
}
I hope this will help you
I suspect 3 posible problems may have created your issue:
Some devices return null when you call extras.get("data"); in onActivityResult method so your problem may be a NullPointerException. To solve this, you need to pass the exact URI location to tell the camera app where you want it to store and use it in onActivityResult to retrieve the image as a Bitmap.
Some other devices return a full size Bitmap when extras.get("data"); in onActivityResult. If the bitmap is too large that can result in an OutOfMemmoryError, so maybe you need to decode your image in a smaller size to not take so much memmory heap. This two links can help you in this situation:
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
BitmapFactory OOM driving me nuts
Maybe your activity is destroyed by the GC so you have to use onSavedInstanceState and onRestoreInstanceState to save your Activity's data. See the answer of this previous post for more information.
I don't know if you already have dealt with these issues.
Hope that helps:)
First, make sure to check request code, you might be catching someone else's result there. Second, don't use the intent's data Bitmap - if anything, it's small, but since you provide the path to store captured image, it shouldn't even be there (see here). So, store the url you provided as output file path and read Bitmap from there when you've received RESULT_OK for your request:
...
// save your file uri, not necessarily static
mUriSavedImage = Uri.fromFile(output);
startActivityForResult(camera, MY_REQUEST_CODE);
/* Make a String like "com.myname.MY_REQUEST_CODE" and hash it into int to give it
a bit of uniqueness */
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Bitmap bmp = BitmapFactory.decodeFile(mUriSavedImage);
if (bmp != null) {
image.setImageBitmap(bmp); // Set imageview to image that was
// captured
image.setScaleType(ScaleType.FIT_XY);
} else {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show();
}
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
For Samsung devices add below one line in your AndroidManifest.xml File
android:configChanges="orientation|screenSize"
i hope this will work for you
I faced the same issue. Apparently the fix is to make the uriSavedImage as static. Not sure if this is the best way. But this works for me.
Follow the steps given on this link. Hope this is useful for you.
OR
Fetching Your Image Without Crashing
Write the below code in MainActivity
// Storage for camera image URI components
private final static String CAPTURED_PHOTO_PATH_KEY = "mCurrentPhotoPath";
private final static String CAPTURED_PHOTO_URI_KEY = "mCapturedImageURI";
// Required for camera operations in order to save the image file on resume.
private String mCurrentPhotoPath = null;
private Uri mCapturedImageURI = null;
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
if (mCurrentPhotoPath != null) {
savedInstanceState.putString(CAPTURED_PHOTO_PATH_KEY, mCurrentPhotoPath);
}
if (mCapturedImageURI != null) {
savedInstanceState.putString(CAPTURED_PHOTO_URI_KEY, mCapturedImageURI.toString());
}
super.onSaveInstanceState(savedInstanceState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.containsKey(CAPTURED_PHOTO_PATH_KEY)) {
mCurrentPhotoPath = savedInstanceState.getString(CAPTURED_PHOTO_PATH_KEY);
}
if (savedInstanceState.containsKey(CAPTURED_PHOTO_URI_KEY)) {
mCapturedImageURI = Uri.parse(savedInstanceState.getString(CAPTURED_PHOTO_URI_KEY));
}
super.onRestoreInstanceState(savedInstanceState);
}
Hello all i know answer has been given but as this is also one of the easiest solution solution i have ever found
Here is an example, which is itself bothering about the device!
AndroidCameraUtils - Download the project and from library project by including it below is the code snippet you can use !
private void setupCameraIntentHelper() {
mCameraIntentHelper = new CameraIntentHelper(this, new CameraIntentHelperCallback() {
#Override
public void onPhotoUriFound(Date dateCameraIntentStarted, Uri photoUri, int rotateXDegrees) {
messageView.setText(getString(R.string.activity_camera_intent_photo_uri_found) + photoUri.toString());
Bitmap photo = BitmapHelper.readBitmap(CameraIntentActivity.this, photoUri);
if (photo != null) {
photo = BitmapHelper.shrinkBitmap(photo, 300, rotateXDegrees);
ImageView imageView = (ImageView) findViewById(de.ecotastic.android.camerautil.sample.R.id.activity_camera_intent_image_view);
imageView.setImageBitmap(photo);
}
}
#Override
public void deletePhotoWithUri(Uri photoUri) {
BitmapHelper.deleteImageWithUriIfExists(photoUri, CameraIntentActivity.this);
}
#Override
public void onSdCardNotMounted() {
Toast.makeText(getApplicationContext(), getString(R.string.error_sd_card_not_mounted), Toast.LENGTH_LONG).show();
}
#Override
public void onCanceled() {
Toast.makeText(getApplicationContext(), getString(R.string.warning_camera_intent_canceled), Toast.LENGTH_LONG).show();
}
#Override
public void onCouldNotTakePhoto() {
Toast.makeText(getApplicationContext(), getString(R.string.error_could_not_take_photo), Toast.LENGTH_LONG).show();
}
#Override
public void onPhotoUriNotFound() {
messageView.setText(getString(R.string.activity_camera_intent_photo_uri_not_found));
}
#Override
public void logException(Exception e) {
Toast.makeText(getApplicationContext(), getString(R.string.error_sth_went_wrong), Toast.LENGTH_LONG).show();
Log.d(getClass().getName(), e.getMessage());
}
});
}
#Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
mCameraIntentHelper.onSaveInstanceState(savedInstanceState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mCameraIntentHelper.onRestoreInstanceState(savedInstanceState);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
mCameraIntentHelper.onActivityResult(requestCode, resultCode, intent);
}
}
NOTE:- I tried many examples for camera utils and ofcourse there are another ways to handle it but for beginners and person who are not too much familier with the core concepts would be more comfort with this project. THanks!
<!-- User features are request to enroll due to vivo solution for mobiles-->
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.front" android:required="true" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="true"/>
<uses-feature android:name="android.hardware.microphone" />
<uses-feature
android:name="android.hardware.camera.any"
android:required="true"/>
<uses-feature
android:name="android.hardware.camera.flash"
android:required="true"/>
<!-- end User features are request to enroll due to vivo solution for mobiles-->