Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to save pictures into system albums in Android

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to save pictures into system albums in Android." Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to save pictures in Android system album"!

Android pictures saved in the system album update display example detailed explanation

In developing android, we couldn't avoid the possibility of creating a custom album or creating a folder locally to store the images we needed. Take the album, for example, we create a test folder, after taking a photo, store it in the specified test folder, and then display it in the album, just like the effect of WeChat. It can be displayed immediately after shooting. However, in the actual development process, we can not immediately update and display this picture after saving a picture, we need to restart the phone to display it in the system album.

Here is a way to insert a system gallery:

MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "", "");

By this method, pictures can be inserted into the system gallery. However, it will not be displayed immediately after insertion. You need to send a broadcast: sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

To inform the system to scan the entire sd card, if the sd inside the more things will scan the longer, and scan we can not access the sd card, so this implementation is not user-friendly.

Here we can scan updates using the method given us by the system api:MediaScannerConnection:

@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHandler.sendEmptyMessageDelayed(1, 2000); mConnection =new MediaScannerConnection(mContext,this);}@Overridepublic void onMediaScannerConnected() { mConnection.scanFile("/sdcard/image.jpg", "image/jpeg");}@Overridepublic void onScanCompleted(String s, Uri uri) { Log.v("huan", "scan completed"); mConnection.disconnect();}

However, I found a problem with this method during use: the incoming context will always be held by MediaScannerConnection, resulting in oom. Therefore, attention should be paid to the use of activity during use.

In addition, we can also broadcast by specifying the path, so that we can avoid scanning the entire sd card:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory() + picPath))); At this point, I believe that everyone has a deeper understanding of "how to save pictures in Android system albums", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report