简体   繁体   English

我想允许用户从我的应用程序下载照片,我在模拟器中的代码是好的,但不是在 apk

[英]I want to allow the user to download photos from my app, my code in emulator is ok ,but not in apk

public void download (View view) {公共无效下载(查看视图){

            Drawable drawable = getResources().getDrawable(getResources()
                    .getIdentifier("bild" + counter, "drawable", getPackageName()));
            bitmap = ((BitmapDrawable) drawable).getBitmap();
                    ImagePath = MediaStore.Images.Media.insertImage(
                    getContentResolver(),
                    bitmap,
                    "bild"+counter,
                    "bild"
            );
            URI = Uri.parse(ImagePath);

            Toast.makeText(MainActivity.this, "Image Saved Successfully", Toast.LENGTH_LONG).show();

        }

unfortunately does not work as an apk不幸的是,它不能用作 apk

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button);
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button3 = (Button) findViewById(R.id.button3);
    button4 = (Button) findViewById(R.id.button4);
    button5 = (Button) findViewById(R.id.button5);
    button6 = (Button) findViewById(R.id.button6);
    button7 = (Button) findViewById(R.id.button7);
    button8 = (Button) findViewById(R.id.button8);
    button9 = (Button) findViewById(R.id.button9);
    imageview2 = (ImageView) findViewById(R.id.imageView2);
    imageview3 = (ImageView) findViewById(R.id.imageView3);
    imageview = (ImageView) findViewById(R.id.imageView);
    webView = findViewById(R.id.webView);



        button2.setOnClickListener(new View.OnClickListener() {
               @Override
                public void onClick(View v) {

                    if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){

                        saveImage();

                    }else {


                        askPermission();

                    }

                }
            });
        }

    private void askPermission() {

        ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_CODE);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull @NotNull String[] permissions, @NonNull @NotNull int[] grantResults) {

        if (requestCode == REQUEST_CODE)
        {

            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){


                saveImage();

            }else {


                Toast.makeText(MainActivity.this,"Please provide the required permissions",Toast.LENGTH_SHORT).show();

            }

        }


        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private void saveImage() {

        File dir = new File(Environment.getExternalStorageDirectory(),"printMyPic");

        if (!dir.exists()){

            dir.mkdir();

        }


        BitmapDrawable drawable = (BitmapDrawable) imageview.getDrawable();
        Bitmap bitmap = drawable.getBitmap();

        File file = new File(dir,System.currentTimeMillis()+".jpg");
        try {
            outputStream = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
        Toast.makeText(MainActivity.this,"Successfuly Saved",Toast.LENGTH_SHORT).show();

        try {
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可以从我的应用程序下载apk并让用户从我的应用程序安装它 - Is is ok to download apk from my app and let the user install it from my app 当我在模拟器上启动我的应用程序时,总是会有一个旧的apk - When I start my app on an emulator there is always an old apk 我想将 pdf 从我的网站下载到我的应用程序中,并且该 pdf 仅显示在我的应用程序中 - I want to download pdf from my website into my app and that pdf only show in my app only 我如何从我的 Apk 大小知道我的应用程序在上传之前的下载大小? - How can I know my App's download size prior Uploading, from my Apk size? 是否可以从 Google Play 下载适用于我的应用程序的旧 APK? - Is it possible to download an old APK for my app from Google Play? 如何允许用户在我的应用iframe中打开或下载PDF文件? - How to allow user to open or download PDF files in my app iframe? 我想从我的 FTP 上传/下载图像到我的 Android 应用程序 - I want to upload/download images from my FTP to my Android App 我想在Android中将照片设置为gridview吗? - I want to set my photos into gridview in an Android? 我想使用Git在PC上下载google camera应用程序源代码 - I want to download google camera app source code on my PC using Git 我想以编程方式从 Android 下载 apk - I want to download apk from Android programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM