I managed to use myimageview.setImageUri when the scheme was content://
But it doesnt work if my Uri has a file:// scheme. What is wrong and how can I make it work?
I have given both read and write permission.
Edit:
To make sure I haven't messed up anything, I created a whole new project.
Mainactivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
ArrayList<Uri> myimageUri = null;
myimageUri = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
Uri theuri = myimageUri.get(0);
ImageView myimageView = findViewById(R.id.imageView);
myimageView.setImageURI(theuri);
}
Attaching a debugger I found out that theuri is
file:///storage/emulated/0/WhatsApp/Media/WhatsApp%20Images/IMG-20171101-WA0005.jpg
It still doesn't work, but content :/// URIs work.
Try following code:
InputStream in = myimageView.g etContext().getContentResolver(). openInputStream(theuri);
Bitmap bitmap = BitmapFactory.decodeStream(in);
in.close();
myimageView.setImageBitmap(bitmap);
You need to decode image file to a bitmap and give it to ImageView.
Bitmap mBitmap = Media.getBitmap(this.getContentResolver(), yourUri);
imageView.setImageBitmap(bitmap);
Related
I'm trying to extract Album art from MP3 file But I not able to give proper location of song, which I can give it to SetDataSource method. I tried everything but still it gives me error IllegalArgumentException.
It all happens when this line metaRetriver.setDataSource(MainActivity.this, Uri.parse("/sdcard/song.mp3")); gets executed
My Code.
public class MainActivity extends AppCompatActivity {
MediaMetadataRetriever metaRetriver;
byte[] art;
ImageView album_art;
Bitmap songImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInit();
metaRetriver = new MediaMetadataRetriever();
metaRetriver.setDataSource(MainActivity.this, Uri.parse("/sdcard/song.mp3"));
art = metaRetriver.getEmbeddedPicture();
if (art != null) {
songImage = BitmapFactory
.decodeByteArray(art, 0, art.length);
album_art.setImageBitmap(songImage);
} else {
String error = "art is null";
Log.i("lolol", error);
}
}
public void getInit() {
album_art = (ImageView) findViewById(R.id.album_art);
}
}
Can anyone solve this problem.
From the Android Documentation:
Throws IllegalArgumentException if the Uri is invalid
Double check that the song.mp3 file is in the right place, and the app has permission to see it.
I'am sorry for the dumb question but I'am still a beginner.
I'am using universal image loader : https://github.com/nostra13/Android-Universal-Image-Loader
(Android, Eclipse)
and I don't know how to make it load my image buttons(nearly 30 imagebuttons per layout), I don't want to change anything in the image I just want it to display it as it is in the xml file.
So if you could tell me what code should I type that would be great, Thanks
for example if I have this ImageButton set up in my java file:
ImageButton staff = (ImageButton) findViewById(R.id.staff);
and my java file is called MainActivity and this image is saved in my drawable folder what should I do.
a part of my code:
public class MainActivity extends Activity implements OnClickListener {
SoundPool sp;
int soundId;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
soundId = sp.load(kh2hd.this, R.raw.btnclick, 1);
Picasso.with(this).load(R.drawable.staff).into(staff);
ImageButton staff = (ImageButton) findViewById(R.id.staffbtn);
staff.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.staffbtn:
sp.play(soundId, 1, 1, 0, 0, 1);
startActivity(new Intent("android.intent.action.INFO"));
break;
Download Picasso library from here
Add it to your build path. Import this library to your code file.
For loading from web url:
String url = "http://example.com"
Picasso.with(getApplicationContext()).load(url).into(imageView);
For loading from drawable:
Picasso.with(getApplicationContext()).load(R.drawable.imageFile).into(imageView);
Here, imageView is the name of variable you are using to represent ImageView. Eg.
ImageView imageView = (ImageView) findViewById(R.id.ImageView1);
If you are using ImageButton, change imageView variable to imageButton variable
ImageButton imageButton = (ImageButton)findViewById(R.id.imageButton);
Note that I am passing variable name.
#user3896367 i think below code is use full for you. if you upload image from drawable of web url using this code.
download universal image loader jar.
put jar in lib folder and add jar.
create application class.
public static void initImageLoader(Context context) {
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(
new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.build();
ImageLoader.getInstance().init(config);
}
4.user imageloader like below syntext.
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.showImageOnLoading(
R.drawable.wallpaper_placeholder)
.showImageForEmptyUri(
R.drawable.wallpaper_placeholder)
.showImageOnFail(R.drawable.wallpaper_placeholder)
.displayer(new FadeInBitmapDisplayer(500))
.cacheInMemory(true).cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565).build();.
imageLoader.displayImage(
URL or ResourceID,
imageButton, options);
try this.
I need some help. I want to refresh my ImageView loaded from a URL in my android app. I try use timers but doesn't work. Any thoughts?
I have this code in Main Activity:
public class Imagens extends Activity {
private ImageView imagem;
private Bitmap bit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imagens);
imagem=(ImageView) findViewById(R.id.imageView);
bit=getBitmapFromURL("http://www.onlinedegrees.org/wp-content/uploads/android1.jpg");
imagem.setImageBitmap(bit);
}
public Bitmap getBitmapFromURL(String src){
try{
URL url = new URL(src);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
}
catch (Exception e){
e.printStackTrace();
return null;
}
}
}
And in the AndroidManifest uses:
uses-permission android:name="android.permission.INTERNET"
to allow internet connection.
I can recommend a different way that works like a charm: Android Query.
You can download that JAR file from here
AQuery androidAQuery = new AQuery(this);
As an example:
androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW);
It's very fast and accurate, and using this you can find many more features like animation when loading, getting a bitmap (if needed), etc.
I am new to android, could any one please tell me how to write effective code with respect to memory management.
public class XXX extends Activity {
EditText et;
ImageView iv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.editText);
iv = (ImageView) findViewById(R.id.iv);
}
public void release(){
et=null;
Drawable drawable = ((ImageView) iv).getDrawable();
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
bitmapDrawable.getBitmap().recycle();
}
iv=null;
System.gc();
}
public void onPic(View v){
// code to capture image
}
public void onSave(View v){
DBHelper dbHelper = new DBHelper(this);
SqliteDatabase db = dbHelper.getReadableDatabase();
String name = et.getText().toString();
ContentValues cv cv= new ContentValues();
// saving , calling release(); and going back to main screen
}
Is there any thing i could do to avoid out of memory, hanging phone?
Bitmap do not easily releases the memory due to which mostly the out of memory exception is caused. You can resize the bitmap and load it.
This might help you which shows efficient bitmap loading:
Bitmap loading
You can go through complete understanding document from here
Bitmap handling
I'm trying to follow this this tutorial to add image into imageView from URL.
public class AndroidLoadImageFromURLActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.image);
String image_url = "http://api.androidhive.info/images/sample.jpg";
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(image_url, image);
}
}
However, I have the image urls stored in my database table and would like to grab and display dynamically in java. please kindly show me how to do this in java? Thank you so much!
try this link
here there is a function named showImage() which accepts a url as parameter and returns bitmap which you can use to set to the image view.
and dont forget to give the internet permission when dealing with images to be displayed from a url