I'm developing an Android application and I do this to save a bitmap into internal storage:
protected void onPostExecute(Bitmap profilePicture)
{
FileOutputStream fOut = null;
try
{
fOut = openFileOutput(Constants.FB_PROFILE_IMAGE_FILE_NAME, Context.MODE_PRIVATE);
profilePicture.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
fOut.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
loadingDialog.dismiss();
}
How can I do to open it and show it into an ImageView?
This code doesn't work because imgFile doesn't exist:
private void loadAndShowUserProfileImage()
{
File imgFile = new File(Constants.FB_PROFILE_IMAGE_FILE_NAME);
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
userImageProfile.setImageBitmap(myBitmap);
}
}
FileInputStream fIn = openFileInput(Constants.FB_PROFILE_IMAGE_FILE_NAME);
Bitmap myBitmap = BitmapFactory.decodeStream(fIn);
openFileOutput/openFileInput should be always used in pair
Related
I have an issue in my Android app saying "Unhandled IOException: java.io.exception" when I'm trying to getBitmap from URI. below is the code.
I do not want to catch all exception using a catch (exception e) as it's too wide.
private void getBitmapFromURI(Uri uri) {
try {
mSnapShot = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
int PreviewSizeWidth = CameraApplication.Instance().getResources().getInteger(R.integer.FilterPreviewWidth);
int PreviewSizeHeight = CameraApplication.Instance().getResources().getInteger(R.integer.FilterPreviewHeight);
mPreviewBitmap = Bitmap.createScaledBitmap(mSnapShot, PreviewSizeWidth, PreviewSizeHeight, false);
FiltersPreview();
}
catch (FileNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "File not found");
finish();
}
}
any idea how to catch exception in case getbitmap not working ?
thx
Try...
try {
mSnapShot = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
int PreviewSizeWidth = CameraApplication.Instance().getResources().getInteger(R.integer.FilterPreviewWidth);
int PreviewSizeHeight = CameraApplication.Instance().getResources().getInteger(R.integer.FilterPreviewHeight);
mPreviewBitmap = Bitmap.createScaledBitmap(mSnapShot, PreviewSizeWidth, PreviewSizeHeight, false);
FiltersPreview();
}
catch (FileNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "File not found");
finish();
}catch(IOException e){
//do something
}
Use the multi catch and make sure to log your exception in logcat. Something like this.
catch (FileNotFoundException | IOException e) {
Log.e(TAG, e.getMessage(), e);
finish();
}
You should also catch the IOException
private void getBitmapFromURI(Uri uri) {
try {
mSnapShot = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
int PreviewSizeWidth = CameraApplication.Instance().getResources().getInteger(R.integer.FilterPreviewWidth);
int PreviewSizeHeight = CameraApplication.Instance().getResources().getInteger(R.integer.FilterPreviewHeight);
mPreviewBitmap = Bitmap.createScaledBitmap(mSnapShot, PreviewSizeWidth, PreviewSizeHeight, false);
FiltersPreview();
}
catch (FileNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "File not found");
finish();
}
catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "IO error");
finish();
}
}
or you have to change you method signature to
private void getBitmapFromURI(Uri uri) throws IOException
and handle this on an other place.
I have an App that is receiving a video file from another App that is working as a Server. While the App is saving the file received on the socket, the video stream starts playing the file (which is under construction). In the code sample, after I press the btnStream, I press the btnPlay and App runs successfully. However, if the playing rate is greater than the download rate, an error will occur. I want to avoid this case. So I need to have a listener on the Video Playing that will pause the videoview when it predicts that this error will occur. I know a solution where if I know the video size, I can counter the bytes received and monitor how many seconds have been buffered and see if the videoview should pause or not. However, is it possible to do it without knowing the video file size? Or having two threads that depends on each other? Thanks.
Note: the VideoView used is a custom one where it can play FileDescriptor.
btnStream.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s = etURL.getText().toString();
String ip = "10.0.0.24";
int port = 7878;
mct= new VideoDownloadTask(ip,port);
mct.execute();
}});
final MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(mVideoView);
Button btnPlay = (Button) findViewById(R.id.button2);
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mVideoView.setVideoFD((new FileInputStream(new File("/sdcard/tempVideo.mp4")).getFD()));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mVideoView.seekTo(0);
mVideoView.start();
}
});
}
public class VideoDownloadTask extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
String response = "";
Socket socket=null;
VideoDownloadTask(String addr, int port){
dstAddress = addr;
dstPort = port;
}
#Override
protected Void doInBackground(Void... arg0) {
try {
socket = new Socket(InetAddress.getByName(dstAddress), dstPort);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
if(socket!=null)socket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
File f = new File("/sdcard/tempVideo.mp4");
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DataInputStream in=null;
try {
in = new DataInputStream (socket.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileOutputStream videoFile = null;
try {
videoFile = new FileOutputStream(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int len;
byte buffer[] = new byte[8192];
try {
while((len = in.read(buffer)) != -1) {
videoFile.write(buffer, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
videoFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
Toast.makeText(getApplicationContext(), "Done Downloading File",
Toast.LENGTH_LONG).show();
super.onPostExecute(result);
}
}
}
I applied a simple solution that resolved the problem. I am sharing it if anyone is having the same problem. The solution was simply to add an error listener to the videoView that will block the error popups and pauses the video.
mVideoView.setOnErrorListener(new OnErrorListener(){
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
statusText.setText("ERROR PLAYING VIDEO");
mVideoView.pause();
return true;
}
});
pDialog = new ProgressDialog(PlayVideoActivity.this);
pDialog.setTitle("Gajacharitra");
pDialog.setMessage("Buffering video...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
try {
// Start the MediaController
mediacontroller.setAnchorView(mVideoView);
// Get the URL from String VideoURL
Uri video = Uri.parse(mVideoURL);
mVideoView.setMediaController(mediacontroller);
mVideoView.setVideoURI(video);
mVideoView.requestFocus();
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
mVideoView.start();
}
});
mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
mVideoView.pause();
pDialog.dismiss();
Toast.makeText(PlayVideoActivity.this, "Can't play this video.", Toast.LENGTH_LONG).show();
finish();
return true;
}
});
} catch (Exception e) {
/*Log.e("Error", e.getMessage());
e.printStackTrace();*/
pDialog.dismiss();
Toast.makeText(PlayVideoActivity.this, "Can't play this video.", Toast.LENGTH_LONG).show();
finish();
}
External storage works, but not internal storage. Am I missing out on something fundamentally from the android storage mechanism?
thanks
public static void playSound()
{
//String path = internalPath + "/www/sounds/" + "SIREN.WAV"; // doesnt work
//String path = "file:///data/data/com.myproject.d08062014f/files/www/sounds/SIREN.WAV"; // doesnt work
//String path = "/data/data/com.myproject.d08062014f/files/www/sounds/SIREN.WAV"; // doesnt work
// all above does not work
Log.d("command", "command:" + path); // to check path string
String path = "file:///mnt/sdcard/media/audio/notifications/facebook_ringtone_pop.m4a"; // This one works!
MediaPlayer mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(path);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mMediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mMediaPlayer.start();
}
Update on 09-10-2014
stackoverflow.com/a/4955915/856007 – Abdullah Shoaib Aug 12 at 7:49
thank you for the reference link below, I was able to get it working with
File file = new File(path); // acquire the file from path string
FileInputStream inputStream = new FileInputStream(file);
mMediaPlayer.setDataSource(inputStream.getFD());
inputStream.close();
I am writing to a file and want console output,
// TODO Create a game engine and call the runGame() method
public static void main(String[] args) throws Exception {
NewGame myGame = new TheGame().new NewGame();
myGame.runGame();
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
}
This gives me console output, but it throws the following exception:
java.io.FileNotFoundException: TheGame.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at game.main(TheGame.java:512)
The file does exist.
The file should be in contained within the root of your project.
When you execute a project in eclipse, the working directory is the most top level of your project.
Right click your project, click New>File, and make a txt file called "TheGame.txt".
// Save Image Code
btnsave = (ImageButton) findViewById(R.id.imageButton1);
btnsave.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//String state = Environment.getExternalStorageState();
URL url=null;
try {
url = new URL (testimage);
} catch (MalformedURLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
input = url.openStream();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
String root = Environment.getExternalStorageDirectory().toString();
File newDir = new File(root + "/KalyanPusti_Images");
newDir.mkdirs();
int n = 10000;
Random gen = new Random();
n = gen.nextInt(n);
String fotoname = tittle+".jpg";
File file = new File (newDir, fotoname);
try {
File storagePath = Environment.getExternalStorageDirectory();
FileOutputStream output = new FileOutputStream (file);
try {
byte[] buffer = new byte[15000];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0){
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally{
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
public File getTempFile(Context context, String url) {
File file =null;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null, context.getCacheDir());
} catch (IOException e) {
// Error while creating file
}
return file;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fullimage, menu);
return true;
}
You need to place it inside the src folder that's same as new FileOutputStream("your-file-name.txt")
-> and if it is inside a folder new FileOutputStream("./your-folder-name/your-file-name.txt")
I have a wav file called 'new.wav' and I want to play it via MediaPlayer and AudioTrack in android:
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "new.wav";
File f = new File(baseDir + File.separator + fileName);
byte[] b = null;
try {
b=this.readFile(f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("123"," "+"length is "+b.length);
truncated=new byte[b.length-44];
truncated=Arrays.copyOf(b, b.length-44);
at=new AudioTrack(AudioManager.STREAM_MUSIC, 48000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, truncated.length /* 1 second buffer */, AudioTrack.MODE_STREAM);
at.write(truncated, 0, truncated.length);
at.play();
mp = new MediaPlayer();
try {
mp.setDataSource(baseDir + File.separator + fileName);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
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();
}
After having tried both methods, I found that their sound is not the same, the AudioTrack is not outputing the original sound in the wav file. I think my sample rate is fine, can someone help me with this issue? THank you!
As I said in the comments above, the problem is the choice of channels.