How to pass drawable between activities
1) Passing in intent as extras
In the Activity A you decode your image and send it via intent:
- Using this method (extras) image is passed in 162 milliseconds time interval
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picture", b);
startActivity(intent);
In Activity B you receive intent with byte array (decoded picture) and apply it as source to ImageView:
Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
2) Saving image file and passing its reference to another activity
- WHY to do so? - taken from http://groups.google.com/group/android-developers/browse_frm/thread/9309931b3f060284#
"The size limit is: keep it as small as possible. Definitely don't put
a bitmap in there unless it is no larger than an icon (32x32 or
whatever).
- In *Activity A* save the file (Internal Storage)
String fileName = "SomeName.png";
try {
FileOutputStream fileOutStream = openFileOutput(fileName, MODE_PRIVATE);
fileOutStream.write(b); //b is byte array
//(used if you have your picture downloaded
// from the *Web* or got it from the *devices camera*)
//otherwise this technique is useless
fileOutStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
- Pass location as String to Activity B
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picname", fileName);
- In *Activity B* retrieve the file
Bundle extras = getIntent().getExtras();
String fileName = extras.getString("picname");
- Make *drawable* out of the picture
File filePath = getFileStreamPath(fileName);
Drawable d = Drawable.createFromPath(filePath.toString());
- Apply it to the ImageView resource
someImageView.setBackgroundDrawable(d);
Passing Drawables between Activities
I suggest you pass the identifier, which is just an integer. Rebuild the drawable as needed in the next activity.
intent.putIntExtra(DRAWABLE_KEY, R.id.your_drawable_id);
Passing an image from an activity to another one
First the mipmap folders are for placing your app/launcher icons (which are shown on the homescreen) in only. Any other drawable assets you use should be placed in the relevant drawable folders.
Next since they will be in your drawables, I would just pass the @DrawableRes id
e.g. the R.id.image_name
value.
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra(IMAGE_RES_ID_KEY, R.id.imageName);
startActivity(intent);
Also I would recommend that you use a public static
variable IMAGE_RES_ID_KEY
for your extra key to avoid typos.
Then on the other side you can simply
if(getIntent().hasExtra(MainActivity.IMAGE_RES_ID_KEY)) {
imageView.setImageResource(getIntent().getIntExtra(MainActivity.IMAGE_RES_ID_KEY, 0));
}
Passing a drawable between Activities image looks Stretched
Why dont you convert the drawable to a bytearray pass the bytearray to the intent via put and then get your drawable back
Pass Drawable from Fragment to Dialogfragment using Bundle
If anyone has the same problem, I used a "workaround" by passing the position of the adapter:
args.putInt("appdrawpos", position);
and compared it with a counter(i) which I use in a method I wrote:
int appDrawPos = 0;
Drawable drawIcon;
int i = 0;
...
//Get the adapter position
appDrawPos = getArguments().getInt("appdrawpos");
//Get apk icon
getAPKicon(folder.getAbsolutePath());
...
public void getAPKicon(String directoryName) {
File directory = new File(directoryName);
final PackageManager pm = getActivity().getPackageManager();
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.isFile()) {
//If counter is the same as adapter position
//Grab the icon from the apk
if (i == appDrawPos)
if (file.getName().endsWith(".apk")) {
PackageInfo info = pm.getPackageArchiveInfo(file.getAbsolutePath(), 0);
info.applicationInfo.sourceDir = file.getAbsolutePath();
info.applicationInfo.publicSourceDir = file.getAbsolutePath();
drawIcon = (info.applicationInfo.loadIcon(pm));
appDrawPos = 0;
i = 0;
}
i++;
} else if (file.isDirectory()) {
getAPKicon(file.getAbsolutePath());
}
}
}
Get Drawable of a button on Another Activity in Android
Assuming that your buttons are all atrological signs, and that you know what image resource you are using for each, why don't you just assign all your buttons the same onClick listener, and use a switch case to determine which sign it is.
public void onClick(View v) {
Intent i = new Intent(this, yourSecondActivity.class);
switch(v.getId()){
case R.id.ariesBtn:
i.putExra("sign", "aries");
break;
case R.id.cancerBtn:
i.putExtra("sign", "cancer");
break;
}
startActivity(i);
}
Then in your other activity check for extras "sign" and act accordingly
I want to Passing a image from Drawable from one activity to Another via Intent?
You are passing an int value in intent. so call
imag_link = getIntent().getIntExtra("valueq", value);
Related Topics
Using "Printf" for Hex Values in Android Shell
"No Such File or Directory" Trying to Execute Linux Binary on Android Device
Android Ndk: Getting the Backtrace
What Kind of Font Files Do I Need for Modern Browsers, Android and iOS
How to Change the Color of Header Bar and Address Bar in Newest Chrome Version on Lollipop
Remove App from Recent Apps Programmatically
No Generated R.Java File in My Project
Android Gridlayout with Dynamic Number of Columns Per Row
How to Place Fragment Arrylist Scrollabletabsactivity
Read Only File System on Android
React-Native: Command Not Found
Export a Python Script to an Android Executable (.Apk) with Python-For-Android
Android Plugin Install Problems in Eclipse
What Is Exactly a "Clock Tick" in the Context of Android CPU Usage