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.
Related
I've created a folder 'test' inside res and I want to display them in an image view. How exactly can I fetch the image with a given name in the designated folder?
ImageView test = (ImageView) testing.findViewById(R.id.test);
flag.setImageDrawable(getResources().); <==== This?
EDIT
InputStream is = null;
try {
is = this.getResources().getAssets().open("country_flags/sample.png");
} catch (IOException e) {
;
}
image = (ImageDrawable) BitmapFactory.decodeStream(is);
try {
flag.setImageDrawable(getResources().getAssets().open("country_flags/"+nationality+".png"));
} catch (IOException e) {
e.printStackTrace();
}
How exactly can I fetch the image with a given name in the designated folder?
You don't. You cannot invent new resource types, and so your test directory will, at best, be forever ignored.
<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?
I am using the following code to take screen shot of current activity:
View rootview = findViewById(android.R.id.content).getRootView();
rootview.setDrawingCacheEnabled(true);
And save the image on the SD card.
But I am trying to take a screenshot of not just this app, but outside this app as well, I have done some research and found that it's possible only with ROOTED devices, but is there any way that we can take a screen shot of any screen without rooting the device?
Try using reflection to call takeScreenshot() from PhoneWindowManager.java Refer to this link
Refer the following code from here
Do this for PhoneWindowManger.
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Log.v(TAG, "Get getTeleService...");
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.silenceRinger();
Log.v(TAG, "Answering Call now...");
telephonyService.answerRingingCall();
Log.v(TAG, "Call answered...");
//telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG,
"FATAL ERROR: could not connect to telephony subsystem");
Log.e(TAG, "Exception object: " + e);
}
Android L offers APIs to capture screenshot.refer-https://developer.android.com/reference/android/media/projection/package-summary.html
You are interested in the READ_FRAME_BUFFER permission: http://developer.android.com/reference/android/Manifest.permission.html#READ_FRAME_BUFFER
Problem is that it clearly states: "Not for use by third-party applications."
You are out of luck. This is a security feature (not allowing any app to take screenshots of other apps) that won't go away. You will be limited to rooted devices.
Here is the code that allowed my screen shot to be stored on sd card and used later for whatever your needs are:
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;
// create bitmap screen capture
Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Then, when you need to access use something like this:
Uri uri = Uri.fromFile(new File(mPath));
Depends on your purpose. If you need that feature in your application which distributed to playstore, you don't have luck. Without root permission, it's simply not possible.
For test or personal purpose, however, adb will help. You just send
some packet to your machine's adb server, you can retrieve device's screen shot.
To do that, you should read out adb client <-> server protocol. framebuffer protocol seems perfect to you.
Or If you familiar with node.js, adbkit will save your time.
Android L offers APIs to capture screenshot.refer-https://developer.android.com/reference/android/media/projection/package-summary.html
When setting the wallpaper I'm using an int and then I'm converting it into a bitmap with decodestream but the wallpaper is still not set.
Here is my code
InputStream y = getResources().openRawResource(friendship);
Bitmap b = BitmapFactory.decodeStream(y);
try {
getApplicationContext().setWallpaper(b);
Toast.makeText(this, "Wallpaper Set!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
Is the permission SET_WALLPAPER set in the manifest file? Android docs link
Further more, the method that you are using is deprecated. You should be using WallpaperManager apis
I'm saving an image from the device's camera to a directory on the SD card (ex: /sdcard/appName/image.jpg), then I save the path into a database. My problem is that I can't seem to load the images into a ListView with a cursor adapter.
I tried the following code, where helper.getImg(); is a method from my database helper that returns a String (the file path), but it is not working.
icon=(ImageView)row.findViewById(R.id.icon_pura);
String imgPath=helper.getImg(c);
Bitmap myBitmap=BitmapFactory.decodeFile(imgPath);
icon.setImageBitmap(myBitmap);
the answer has to do with URIs, and you need to use the externalstorage() function. Using set paths won't work on every device
anyway you store path into the URI which is more flexible for retrieving and parsings items at that path
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Directory name/";
File file = new File(filepath,imagename);
FileInputStream fs = null;
try
{
fs = new FileInputStream(file);
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapFactory.Options bfOptions = new BitmapFactory.Options();
/*
* bfOptions.inDither=false; //Disable Dithering mode
* bfOptions.inPurgeable=true; //Tell to gc that whether it needs
* free memory, the Bitmap can be cleared
* bfOptions.inInputShareable=true;*/
bfOptions.inJustDecodeBounds = false;
bfOptions.inTempStorage = new byte[32 * 1024];
try {
Bitmap originalImage = BitmapFactory.decodeFileDescriptor(fs.getFD(), null,bfOptions);
icon.setImageBitmap(originalImage);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
If you follow himanshu's (correct) advice, make sure that if you're going to allow the user the option of loading and re-loading the image, make sure to manually icon.setImageBitmap(null); between loads, because otherwise Android will leak that memory and you'll crash your app. It's not 100% consistant, and has something to do with the sizes of the images you're loading, but I just found this leak a few days ago and am 100% certain that it's there.