دانلود عکس و فیلم و نمایش آن در اندروید استودیو

سوال

سلام چطور میتونم یه برنامه درست کنیم با اندروید استودیو ک با دادن لینک به برنامه و کلیک روی دکمه عکس یا فیلم مورد نظرمون دانلود بشه؟مثلا عکس ها و فیلم های روبینو در روبیکا یا اینستاگرام

در حال بررسی 0
, 2019-10-04T05:59:08+03:30 1 پاسخ ها کاربر تازه 0

پاسخ ( 1 )

    1
    2019-10-04T11:53:58+03:30

    سلام

    یه آموزش نسبتا جزئی و ساده براتون می نویسم، بعدا خودتون میتونید شخصی سازی کنید.

    • ابتدا یک پروژه ایجاد و اسم آن را beporsamImage میزاریم.
    • سپس در MainActivity.java کدهای زیر رو میزاریم.
    package com.example.beporsamImage;
    import java.io.InputStream;
    
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.text.Editable;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity {
    
    public String URL="";
    EditText editText;
    ImageView image;
    Button button;
    ProgressDialog mProgressDialog;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the layout from image.xml
    setContentView(R.layout.activity_main);
    
    editText=(EditText)findViewById(R.id.edittxt);
    
    // Locate the ImageView in activity_main.xml
    image = (ImageView) findViewById(R.id.image);
    
    // Locate the Button in activity_main.xml
    button = (Button) findViewById(R.id.button);
    
    // Capture button click
    button.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {
    
    //get the image url from the text box
    URL=editText.getText().toString();
    
    // Execute DownloadImage AsyncTask
    new DownloadImage().execute(URL);
    }
    });
    }
    
    // DownloadImage AsyncTask
    private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
    
    @Override
    protected void onPreExecute() {
    super.onPreExecute();
    // Create a progressdialog
    mProgressDialog = new ProgressDialog(MainActivity.this);
    // Set progressdialog title
    mProgressDialog.setTitle("Download Image Using AsyncTask Tutorial");
    // Set progressdialog message
    mProgressDialog.setMessage("Downloading...please wait...");
    mProgressDialog.setIndeterminate(false);
    // Show progressdialog
    mProgressDialog.show();
    }
    
    @Override
    protected Bitmap doInBackground(String... URL) {
    
    String imageURL = URL[0];
    
    Bitmap bitmap = null;
    try {
    // Download Image from URL
    InputStream input = new java.net.URL(imageURL).openStream();
    // Decode Bitmap
    bitmap = BitmapFactory.decodeStream(input);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return bitmap;
    }
    
    @Override
    protected void onPostExecute(Bitmap result) {
    // Set the bitmap into ImageView
    image.setImageBitmap(result);
    // Close progressdialog
    mProgressDialog.dismiss();
    }
    }
    }

    در قطعه کد بالا یک EditText، یک دکمه و ImageView قرار دارد که با کلیک روی دکمه تصویر از آدرس دانلود و تا زمانی که تصویر میخواد دانلود بشه یک Progress dialog نمایش داده میشه و تصویر دانلود و در ImageView نمایش داده میشه.

    • سپس در activity_main.xml کدهای مربوط به لایوت را می نویسیم.
    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <EditText
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/edittxt"
    android:hint="آدرس تصویر را بنویسید"
    />
    
    <Button
    android:id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/image"
    android:text="دانلود عکس"
    android:paddingBottom="50dp"
    android:gravity="center"/>
    <ImageView
    android:id="@+id/image"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    />
    </LinearLayout>
    • حالا رشته های ترجمه را در فایل strings.xml باید اضافه کنیم.
    <resources>
    <string name="app_name">نرم افزار دانلود عکس بپرسم</string>
    <string name="menu_settings">تنظیمات</string>
    <string name="button">دانلود عکس</string>
    </resources>
    • و در انتها باید دسترسی ها را به AndroinManifest اضافه کنیم، که به صورت زیر میشود:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.beporsamImage">
    <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="27" />
    <uses-permission android:name="android.permission.INTERNET" >
    </uses-permission>
    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    </application>
    </manifest>

    حالا اگه از برنامه خروجی بگیرید، یک EditText بایک دکمه مشاهده میکنید، که پس از اینکه آدرس عکس را در EditText گذاشتید با زدن دکمه؛ عکس دانلود و در ImageView نمایش داده میشه.

    برای ویدئو هم به همین شکل میتونید عمل کنید.

    موفق باشید.

ارسال یک پاسخ