How do i use this method in kitkat - android

I have tried many different ways or solutions and none of them seems to work. I have tried a few specific solutions which returns me a string and a context but i do not know what to do with the context even if I set the receiver of the context as null the app returns an error. What I want to do is, to be able to upload an image file for that I need the file path, a Uri or a content Uri gives me addresses like this
Content://something_something/304:
But i need something like this "
storage/sdcard/something_something/304.jpg"
how do it get that in KITKAT?
This is the code/method that I use for getting the path to the selected user Image.
public String getPath(Uri uri)
{
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
hope i provided enough details. Thank you for any help.

KITKAT does not provide the actual image path for security purpose.
if (Build.VERSION.SDK_INT >19){
InputStream imInputStream = getContentResolver().openInputStream(data.getData());
Bitmap bitmap = BitmapFactory.decodeStream(imInputStream);
String imagePath = saveGalaryImageOnKitkat(bitmap);
}
///After use you can delete the bitmap.
private String saveGalaryImageOnKitkat(Bitmap bitmap){
try{
File cacheDir;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
cacheDir=new File(Environment.getExternalStorageDirectory(),getResources().getString(R.string.app_name));
else
cacheDir=getExternalFilesDir(Environment.DIRECTORY_PICTURES);
if(!cacheDir.exists())
cacheDir.mkdirs();
String filename=java.lang.System.currentTimeMillis()+".png";
File file = new File(cacheDir, filename);
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
return file.getAbsolutePath();
}catch(Exception e){
e.printStackTrace();
}
return null;
}

Related

converting audio file to bytearray: no such file or directory

Im trying to convert an audio file to byte array. where the audio file is fetch through attach file so its in an Uri
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis;
try {
//FileInputStream = reading the file
fis = new FileInputStream(new File(audioFxUri.getPath()));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
} catch (Exception e) {
e.printStackTrace();
}
byte[] bbytes = baos.toByteArray();
i followed that code from this one but once i ran. It gave me an error
W/System.err: java.io.FileNotFoundException: /external/audio/media/646818: open failed: ENOENT (No such file or directory)
I also tried to play it with a MediaPlayer and place Uri as its Source
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(AttachAudio.this,audioFxUri.toString());
player.prepare();
player.start();
to make sure if the audio file is working or not. Ps. it worked.
Is there something wrong im doing as to why it didnt convert to byte array?
The File Uri you have /external/audio/media/646818 is not an actual File Path, hence it is giving FileNotFoundException.
You need to get absolute path from the URI and then read the file accordingly.
You can either use below code:
public String getRealPathFromURI(Uri contentUri)
{
String[] proj = { MediaStore.Audio.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
or Refer this or this SO link for other options/methods

how to store an image path for an image loaded in imageview android

the user will select an image that will later be uploaded on his command without selecting the image themselves. I have tried some of the option on SO but the option i used seems to be replicating a black image and storing its own path. I need the same image to be used and uploaded please help.
the code for the saving is as follows:
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
and the code i am using to then get this image later is:
Bitmap bitmap = BitmapFactory.decodeFile(submissions.getLinkToDoc());
documents.setImageBitmap(bitmap);
where submissions.getLinkToDoc() is the path stored by the code above.

How to get Uri from Bitmap (Lollipop)

Ok, I tried this code:
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
and looks like it can't do the job. When I log it it prints:
content://media/external/images/media/25756
which doesn't helps me because I need it for new File(Uri). Strangely, getImageUri method returns Uri, but File doesn't seem to recognize it. Anyone has method to retrieve Uri from freshly made bitmap?
P.S. As far as I read, it doesn't work since KitKat.
edit
This code works and from this I want to retrieve Uri:
croppedBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
cropImageView = (CropImageView) findViewById(R.id.CropImageView);
cropImageView.setImageBitmap(croppedBitmap);
Button crop = ...;
crop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cropContainer.setVisibility(View.GONE);
mImageView.setVisibility(View.VISIBLE);
Bitmap croppedImage = cropImageView.getCroppedImage();
mImageView.setImageBitmap(croppedImage);
paramsContener.setVisibility(View.VISIBLE);
}
});
and looks like it can't do the job
That would depend upon what "the job" is.
When I log it it prints: content://media/external/images/media/25756 which doesn't helps me because I need it for new File(Uri)
First, if you want to save a bitmap to a file, use a FileOutputStream rather than a ByteArrayOutputStream. Java file I/O has been around for ~20 years.
Second, if you are expecting to get file paths from a ContentProvider like MediaStore, you are going to be disappointed. You get back a Uri, like the content:// one in your question. A Uri is not a file. Use a ContentResolver and methods like openInputStream() to read in the data identified by that Uri. The concept of a "URI" representing an opaque handle to something you get via a stream has been around at least as long as has HTTP, which has been around for ~25 years.
Strangely, getImageUri method returns Uri, but File doesn't seem to recognize it.
That is because a Uri is not a file.
Okay, I got it thanks to #CommonsWare - made something like this. Hope will help it another people as well:
public String makeBitmapPath(Bitmap bmp){
// TimeStamp
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "Photo"+ timeStamp +".jpg"); // the File to save to
try {
fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close(); // do not forget to close the stream
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
} catch (IOException e){
// whatever
}
return file.getPath();
}

Image Uri to File

I've got an Image Uri, retrieved using the following:
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
This works just amazing for Intents that require an Image URI, etc (so I know for sure the URI is valid).
But now I want to save this Image URI to a file on the SDCARD. This is more difficult because the URI does not really point at a file on the SDCARD or the app.
Will I have to create a bitmap from the URI first, and then save the Bitmap on the SDCARD or is there a quicker way (preferable one that does not require the conversion to a bitmap first).
(I've had a look at this answer, but it returns file not found - https://stackoverflow.com/a/13133974/1683141)
The problem is that the Uri you've been given by Images.Media.insertImage() isn't to an image file, per se. It is to a database entry in the Gallery. So what you need to do is read the data from that Uri and write it out to a new file in the external storage using this answer https://stackoverflow.com/a/8664605/772095
This doesn't require creating a Bitmap, just duplicating the data linked to the Uri into a new file.
You can get the data using an InputStream using code like:
InputStream in = getContentResolver().openInputStream(imgUri);
Update
This is completely untested code, but you should be able to do something like this:
Uri imgUri = getImageUri(this, bitmap); // I'll assume this is a Context and bitmap is a Bitmap
final int chunkSize = 1024; // We'll read in one kB at a time
byte[] imageData = new byte[chunkSize];
try {
InputStream in = getContentResolver().openInputStream(imgUri);
OutputStream out = new FileOutputStream(file); // I'm assuming you already have the File object for where you're writing to
int bytesRead;
while ((bytesRead = in.read(imageData)) > 0) {
out.write(Arrays.copyOfRange(imageData, 0, Math.max(0, bytesRead)));
}
} catch (Exception ex) {
Log.e("Something went wrong.", ex);
} finally {
in.close();
out.close();
}

Android MediaRecorder and setOutputFile

I've read the Android SDK and I've found that the MediaRecorder class can take input from a Camera, Audio or other source and compress it. Through the setOutputFile method you can specify where you want the data to be stored (File or URI), but what if I want to store that data in a memory buffer and send it over a connection? Or process it before sending it? I mean is there a way not to create a file but to use a memory buffer only?
You can of course read the file in later and do whatever you want with it in the way of processing. Assuming that u holds the Uri to the resulting audio file, here is a code snippet that reads it into a byte array and then deletes the file.
String audioUri = u.getPath();
InputStream in = new BufferedInputStream(this.getContentResolver().openInputStream(u));
byte[] b = new byte[BUFSIZE];
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(mFileName/*mFilePath*/)));
int byteCnt = 0;
while (0 <= (byteCnt = in.read(b, 0, BUFSIZE)))
out.write(b, 0, byteCnt);
out.flush();
out.close();
// try to delete media file
try {
// Delete media file pointed to by Uri
new File(getRealPathFromURI(u)).delete();
} catch (Exception ex) {}
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

Categories

Resources