这个问题在这里已经有答案了:
我可以在android studio中使用不推荐的方法和类吗(3个答案)
如何为不推荐的方法找到替换方法(2个答案)
mediastore.images.media.insertimage已弃用(1个答案)
17天前关门了。
package com.MahtoBrothers.Indobuddy.Inner_VP_Package.Add_picture_Package;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.TouchDelegate;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import com.MahtoBrothers.Indobuddy.CodeClasses.RecyclerItemClickListener;
import com.MahtoBrothers.Indobuddy.CodeClasses.Variables;
import com.MahtoBrothers.Indobuddy.CodeClasses.ViewPager_Adp;
import com.MahtoBrothers.Indobuddy.Main_VP_Package.MainActivity;
import com.MahtoBrothers.Indobuddy.R;
import com.MahtoBrothers.Indobuddy.SharedPrefs.SharedPrefrence;
import com.MahtoBrothers.Indobuddy.Utils.filter_page;
import com.google.android.material.tabs.TabLayout;
import com.theartofdev.edmodo.cropper.CropImageView;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
public class Gallery extends Fragment {
ArrayList<String> listOfAllImages = new ArrayList<String>();
View view;
RecyclerView rv;
Gallery_Adp adp;
ImageView myImage, ic_back;
Bitmap but_selected_img;
String path;
TextView Next;
ViewPager_Adp view_pager_adp;
private CropImageView mCropImageView;
ViewPager vp;
private Uri mCropImageUri;
TabLayout tl;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.gallery, container, false);
myImage = view.findViewById(R.id.image);
tl = (TabLayout) view.findViewById(R.id.tablayout_id);
ic_back = view.findViewById(R.id.iv_id);
mCropImageView = (CropImageView) view.findViewById(R.id.CropImageView);
mCropImageView.setAspectRatio(1,1);
mCropImageView.setFixedAspectRatio(true);
Next = view.findViewById(R.id.tv_id);
final View parent_2 = (View) Next.getParent(); // button: the view you want to enlarge hit area
parent_2.post( new Runnable() {
public void run() {
final Rect rect = new Rect();
Next.getHitRect(rect);
rect.top -= Variables.click_area_top_100; // increase top hit area
rect.left -= 200; // increase left hit area
rect.bottom += Variables.click_area_bottom_100; // increase bottom hit area
rect.right += Variables.click_area_right_200; // increase right hit area
parent_2.setTouchDelegate( new TouchDelegate( rect , Next));
}
});
view_pager_adp = new ViewPager_Adp(getChildFragmentManager());
// view_pager_adp.add_fragment(new Gallery(), "Gallery");
// // adp.add_fragment(new Photo(), "Photo");
// //adp.add_fragment(new Video(), "Video");
// vp.setOffscreenPageLimit(1);
// vp.setAdapter(adp);
//
// tl.setupWithViewPager(vp);
// tl.getTabAt(0).select();
ic_back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// your handler code here
// ((MediaGallery) getContext()).finish();
// getActivity().finish();
//
// Intent intent = new Intent(getActivity(), MainActivity.class);
// startActivity(intent);
getFragmentManager().popBackStack();
//getActivity().getFragmentManager().popBackStack();
}
});
Next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try{
get_crop_Bitmap();
}catch (Exception b){
}
}
});
rv = (RecyclerView) view.findViewById(R.id.rv_id);
adp = new Gallery_Adp(getContext(), getAllShownImagesPath(getActivity()));
rv.setLayoutManager(new GridLayoutManager(getContext(), 4));
rv.setHasFixedSize(false);
rv.setAdapter(adp);
rv.addOnItemTouchListener(
new RecyclerItemClickListener(getContext(), rv ,new RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) {
// do whatever
// Methods.toast_msg(getContext(),"Pos " + position + " Data " + listOfAllImages.get(position));
try{
path = listOfAllImages.get(position);
// Methods.toast_msg(getContext(),"" + path.toString());
// TODO add Cropping image
mCropImageView.setImageUriAsync(Uri.fromFile(new File(path)));
// Methods.toast_msg(getContext(),"" + path);
SharedPrefrence.save_info_share(
getContext(),
"" + path,
"" + SharedPrefrence.share_media_upload_path_key
);
// Methods.loadBitmap(path);
}catch (Exception v){
// Methods.toast_msg(getContext(),"" + v.toString());
// Methods.toast_msg(getContext(),"" + v.toString());
}
}
@Override public void onLongItemClick(View view, int position) {
}
})
);
return view;
}
public ArrayList<String> getAllShownImagesPath(Activity activity) {
Uri uri;
Cursor cursor;
int column_index_data, column_index_folder_name;
String absolutePathOfImage = null;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.MediaColumns.DATA,MediaStore.Video.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
cursor = activity.getContentResolver().query(uri, projection, null,
null, null);
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
column_index_folder_name = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data).trim();
listOfAllImages.add(absolutePathOfImage);
}
String[] projection1 = { MediaStore.Video.VideoColumns.DATA ,MediaStore.Video.Media.DISPLAY_NAME};
Cursor cursor1 = getContext().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection1, null, null, null);
try {
cursor1.moveToFirst();
do{
String path = cursor1.getString(cursor1.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
// Methods.toast_msg(getContext(),"" + path);
// listOfAllImages.add((cursor1.getString(cursor1.getColumnIndexOrThrow(MediaStore.Video.Media.DATA))));
}while(cursor1.moveToNext());
mCropImageView.setImageUriAsync(Uri.fromFile(new File(listOfAllImages.get(0))));
cursor1.close();
} catch (Exception e) {
e.printStackTrace();
}
return listOfAllImages;
}
public void get_crop_Bitmap(){
// Get Cropped Bitmap
// Bitmap cropped = mCropImageView.getCroppedImage(500, 500);
Bitmap cropped = mCropImageView.getCroppedImage(300,300);
// if (cropped != null)
// mCropImageView.setImageBitmap(cropped);
// mCropImageView.get
// bitmap = Bitmap.createScaledBitmap(bitmap1, 512, 400, false);
// ByteArrayOutputStream bStream = new ByteArrayOutputStream();
// cropped.compress(Bitmap.CompressFormat.PNG, 100, bStream);
// byte[] byteArray = bStream.toByteArray();
Uri tempUri = getImageUri(getContext(), cropped);
final String path = getRealPathFromURI(tempUri);
String oo = Uri.parse(path).toString();
// todo: Here is Crash
try{
Intent anotherIntent = new Intent(getContext(), filter_page.class);
anotherIntent.putExtra("image", oo);
startActivity(anotherIntent);
}catch (Exception v) {
}
// ((Activity)getActivity()).finish();
}
public boolean onBackPressed() {
getActivity().finish();
Intent intent = new Intent(getContext(), MainActivity.class);
startActivity(intent);
return true;
}
@Override
public void onStart(){
super.onStart();
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, System.currentTimeMillis() + "_profile.jpg", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContext().getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
}`
这个代码很好用。但是,在将sdk版本更新为29之后,insertimage方法被弃用。当我查看医生的时候,我看到了这样的陈述:
此方法在api级别29中已被弃用。图像的插入应使用 MediaColumns#IS_PENDING
,它提供了更丰富的生命周期控制。
暂无答案!
目前还没有任何答案,快来回答吧!