created images not recognized in android gallery - android

I'm creating an app that creates and saves images. Currently I am saving them to /sd_card/my_app_name/. The files are there and everything, but they are not recognized from the Android's gallery. Is there any way to fix this?
Thanks!
well, I figured this out quickly enough :
try {
MediaStore.Images.Media.insertImage(getContentResolver(), filePath, "image name", "image dexscription"); } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Use below code to make every new change recognized.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
I think this will resolve your issue.

The way to do this is this:
MediaStore.Images.Media.insertImage(getContentResolver(), filePath, image name, details);

Related

Android : Reading a file from my app by just clicking on it

I have this issue:
I want to create an Android App that will read a certain file type (as is the case in PDF or MP3 players), the problem is that I dont know how to read a file with my application just by clicking on it
if anyone knows how to do this in android please help
i well be grateful for your help, thank you for your time and consideration and so sorry for my english
String outputFile = "my_patch.mp3";
button_play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//this content
MediaPlayer m = new MediaPlayer();
try {
m.setDataSource(outputFile);
}
catch (IOException e) {
e.printStackTrace();
}
try {
m.prepare();
}
catch (IOException e) {
e.printStackTrace();
}
m.start();
}
});
and open file pdf, you make library pdf-view to android studio https://github.com/jblough/Android-Pdf-Viewer-Library
good luck..

How to copy files in Android Storage?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I am trying to copy .jpg file to and from in Storage folders.
try {
Process c = Runtime.getRuntime().exec("dd if="+path+
"of="+Environment.getExternalStorageDirectory()+"/1.jpg");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
path contains the exact image location in storage ! But it doesn't work. It can't copy the image to another folder of storage. Please kindly help me to solve this in every possible way! Thank you.
try {
String cmd="dd if="+path+" of="+Environment.getExternalStorageDirectory()+"/1.jpg";
Runtime.getRuntime().exec(cmd+"\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
It happens to me sometimes and I use
Environment.getExternalStorageDirectory().getPath()
Have you tried that?

MediaPlayer - can't find path of sdcard on real device

On my code I use:
mp = new MediaPlayer();
String filePath = Environment.getExternalStorageDirectory().getPath() + "/mymusic/asong.mp3";
try {
mp.setDataSource(filePath);
} catch (IOException e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
And on the emulator the song.mp3 is played normally. But when I test it on my real device, it gives an error (-38, 0). That means it can not find the path of the song. I connect the usb cable, go to my Computer, GT-I8260 and paste the folder "mymusic" (that contains asong.mp3) under "Card" folder (where an empty folder named "LOST.DIR" is also placed). But why doesn't it work? Thanks a lot
it's card but at least
Environment.getExternalStorageDirectory() + "/mymusic/asong.mp3";
is enough.
Make sure it exists because you may not have the folder created before.
File f = new File(Environment.getExternalStorageDirectory() + "/mymusic");
if (!f.exists()) { f.mkdirs(); }
also make sure that it's not mounted while writing, since it may happen that it is not accessable at all.
Also revalidate that you have setupted the manifests permission to read/write the external storage

Pictures wont save on Samsung Nexus

I have a Problem. I start from my own Application the Build-In Photoapplication with a Photo-Intent.
String photoName = System.currentTimeMillis() + ".jpg";
File file = new File(getFilesDir(),//Environment.getExternalStoragePublicDirectory(
//Environment.DIRECTORY_DCIM),
photoName); // Anlegen der Datei im entsprechenden
// Verzeichnis
FileOutputStream fos = null;
try {
fos = openFileOutput(photoName, Context.MODE_WORLD_WRITEABLE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
outputFileUri = Uri.fromFile(file);
intentPhoto.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
//startActivity(intentPhoto);
startActivityForResult(intentPhoto, TAKE_PICTURE);
This is my Code to start the Activity. As you can see, the first thing i do is to set up the file in the directory and then give the intent the location of the file to store it.
But everytime i take a picture it is not saved to the Pictures Directory. The only way to save the picture is to turn off the phone and restart it. Then every picture I have taken befor is there. This happens since the last updated to 4.1.1. Befor i updated the phone, everything worked fine, but since the update I have this problem.
Can somebody help me? Does anyone have the same Issue?
Make sure you offer the new file to be scanned:
MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
// code to execute when scanning is complete
}
});
You can pass in a null argument for the OnScanCompletedListener if you don't need to be notified when the scanner has picked it up, but you might want to at least put a logging statement there.
It works fine now, the problem was that I create the File by my self, with:
File file = new File(..);
But you have to use:
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "IMG_"+ timeStamp+.jpg");
And put it into the intent to save the file at once without restarting the phone.

Android bitmap in path

I am wondering how to get the bitmap from specific location as follows:
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("C:/java/AVMOrderServer/files/main_ads/fff.jpg").getContent());
iv.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Any advice would be useful (I found the code sample on stackoverflow which is an answer voted as true)
Thank you all.
Your Android device does not have a C drive, that's generally a DOS/Windows concept. Android is based on Linux. If you want to view a bitmap on your computer from your Android device you'll have to copy it over first, whether by including it in your APK or by copying to SD card.

Categories

Resources