Trying to load and read images from SD card android - java

What I am trying to do:
I am trying to load all the images that I have onto the phones SD card when the app is created. I then have a button and when I click the button, the next image should show.
What I've done:
So far I have included this code in the onCreate() method, however I don't know how to add multiple images into the folder.
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory, "uct.png");
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
At the moment I have an array of of image IDs, these images are stored in the Drawable folder at the moment.
int[] images = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
Then I have an onClick() method that increments a private index variable to set the Image Resource like so (here imgView is the ImageView).
imgView.setImageResource(images[current_image_index]);
What I don't know how to do:
Firstly, I don't know how to add multiple images to the SD card in the onCreate() method. Also I am not sure what the best way to go about loading the 'correct' image from the SD when the button is clicked.
I hope this makes sense, please remark if anything needs to be clarified.

If i were in your case,
Create the ViewPager to show all images.
And you can get all images url using this code.
String[] STAR = {"*"};
ArrayList<String> imageUri = new ArrayList<>();
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI
, STAR, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String path = cursor.getString(cursor
.getColumnIndex(MediaStore.Images.Media.DATA));
Log.i("Path", path);
imageUri.add(path);
getDate(path);
} while (cursor.moveToNext());
}
}
You can pass the imageUri ArrayList to Viewpager adapter.At viewPager
adpter you can use any image library to load image. In case of Picasso.
Picasso.with(context)
.load(uri)
.resize(150,150)
.centerCrop()
.into(imageView);

Related

Display GIF as button compound drawable programmatically

For the past few days, I am trying to set the icon of one of my Buttons as a GIF file programmatically. My GIF is located in my assets.
This is the code that I tried. It partially works, but the GIF seems to be stopped and is not playing, like just an ordinary PNG file.
AssetManager assetManager = context.getAssets();
Drawable drawable = null;
try {
drawable = Drawable.createFromStream(context.getAssets().open("test.gif"), null);
button.setCompoundDrawablesWithIntrinsicBounds(drawable, 0, 0, 0)
} catch (IOException e) {
e.printStackTrace();
}
I am expecting the drawable to be playing, just like a GIF.
Any help is appreciated. Thank you.
AssetManager assetManager = context.getAssets();
GifDrawable drawable = null;
try {
drawable = new GifDrawable(context.getAssets(), path);
button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
} catch (IOException e) {
e.printStackTrace();
}
Hey, hope this helps you with your question, you could add this to your .gradle file.

Screenshot not captured while agora.io video call Android Java

I have implemented a video calling feature in our app. There is a button which captures the screenshot of specific view. On video call there a two views Local View and Remote View. These are actually Relative and Frame layouts. In this case i want take screenshot of remote view but it's just giving me a blank image. I think somehow agora video calling not allowing it but there should be any settings or something to disable this but that i have not found yet. here is the code:
Setting up remote video:
mRemoteContainer is a RelativeLayout and mRemoteVideo is VideoCanvas by agora.io
Similarly mLocalContainer is FrameLayout.
private void setupRemoteVideo(int uid) {
ViewGroup parent = mRemoteContainer;
if (parent.indexOfChild(mLocalVideo.view) > -1) {
parent = mLocalContainer;
}
if (mRemoteVideo != null) {
return;
}
SurfaceView view = RtcEngine.CreateRendererView(getBaseContext());
view.setZOrderMediaOverlay(parent == mLocalContainer);
parent.addView(view);
mRemoteVideo = new VideoCanvas(view, VideoCanvas.RENDER_MODE_HIDDEN, uid);
mRemoteVideo.view.setWillNotDraw(false);
mRtcEngine.setupRemoteVideo(mRemoteVideo);
}
This function used to get bitmap from the view
private Bitmap getScreenShotFromView(View v) {
Bitmap screeShoot = null;
try {
screeShoot = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(screeShoot);
v.draw(canvas);
} catch (Exception e) {
Log.e("GFG", "Failed to capture screenshot because:" + e.getMessage());
}
return screeShoot;
}
I tried function call with both view but getting black or blank image
Bitmap bitmap = getScreenShotFromView(mRemoteVideo.view);
Bitmap bitmap = getScreenShotFromView(mRemoteContainer);
Your help will be really appreciated for me as i stuck on this issue.
By the way I used the same view capturing technique on iOS with swift
and Its working fine.

How to save a bitmap after applying effects

I have some buttons and one image view. I load a photo from gallery,the photo is displayed on image view. For every button,there is on click method to apply that applies one of the effects for the image. There is another button,that is for saving the photo on internal storage. I want to save the photo after one of the effects is applied. I searched,but I just find how to save the photo without effect . Does somebody know how with the on click method to save the image AFTER the effect is applied .
you can simply get the bitmap from ImageView by calling this function:
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap()
and then save your bitmap where you want with in/out streams:
private void saveBitmapFromImageView(File destFile, ImageView imageView){
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap()
OutputStream os;
try {
os = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
} catch(Exception e) {
Log.e(TAG, "saveBitmapFromImageView", e);
} finally {
IOUtils.closeQuietly(os);
}
}
Edit #1:
IOUtils is Apache commons lib, add to your gradle:
compile 'org.apache.commons:commons-io:1.3.2'

How to show a captured image in an ImageView?

I'm trying to display a captured image from a camera intent, but when I call a showPhoto method the image isn't shown in the ImageView. There is no error output to the stack trace so I'm not sure how the issue can be debugged.
Does anyone know what is wrong with the method that its not showing the captured image?
The showPhoto method is as follows:
private void showPhoto(Uri photoUri) {
String filePath = photoUri.getEncodedPath();
File imageFile = new File(filePath);
//File imageFile = new File(photoUri.getPath());
if (imageFile.exists()){
Drawable oldDrawable = photoImage.getDrawable();
if (oldDrawable != null) {
((BitmapDrawable)oldDrawable).getBitmap().recycle();
}
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
BitmapDrawable drawable = new BitmapDrawable(this.getResources(), bitmap);
photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
photoImage.setImageDrawable(drawable);
}
}
This is the tutorial that I've followed: http://www.linux.com/learn/tutorials/722038-android-calling-the-camera
The device I'm testing on is JellyBean 4.1 and this is the link to the complete class:
http://hastebin.com/oqokupulol.java
Use BitmapOptions.inJustDecodeBounds to find out the dimension of the image first
Typically, Android won't load images larger than 4000px wide/tall
If you find out the image is too large, you can use BitmapOptions.inSampleSize to downscale the image so that Android would load it
And in fact, you can directly use ImageView.setImageBitmap(Bitmap) instead of setImageDrawable

How to load image from URL in Fragment class?

I want to load an image from its URL in Fragment class. I'm loading the image like this:
try {
// ImageView j=(ImageView)rootView.findViewById(R.id.image_frm);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(image_url).getContent());
Bitmap resized = Bitmap.createScaledBitmap(bitmap, 600, 600, true);
Bitmap conv_bm = getRoundedRectBitmap(resized, 255);
i.setImageBitmap(conv_bm);
// j.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
but this method is taking more time to load the image and to open it.
If I want to use ImageLoader class like this tutorial, then how to load it in Fragment?
Use any 3rd party library like Picasso. Download the .jar file and put the it inside your libs folder. For more info see Picasso's documentation.
in case of Fragments, use getActivity() instead of context.
For rounded ImageView, I use the RoundedImageView lib.

Categories